问题
I have 4 items in my QCombobox
,
'Bryce king'
'James White'
'Russo W'
'Custom Manager'
So, when I click on "Custom Manager"
, it should change to editable and I must be able to enter my own desired name.
I have tried to achieve this behavior using QtCreator, and in the properties, I can set it to editable but that would make all items editable instead of just one.
回答1:
First make sure your combobox's editable property is set to false.
Then you can use the QComboBox::currentIndexChanged signal and check the value of the current index and then make the combobox editable. Example:
void MainWindow::on_comboBox_currentIndexChanged(const QString &arg1)
{
if (arg1 == "Custom Manager") {
ui->comboBox->setEditable(true);
}
else {
ui->comboBox->setEditable(false);
}
}
Moreover, if you want to save the value to the edited text use QComboBox::currentTextChanged signal:
void MainWindow::on_comboBox_currentTextChanged(const QString &arg1)
{
qDebug() << arg1;
ui->comboBox->setItemText(ui->comboBox->currentIndex(), arg1);
}
来源:https://stackoverflow.com/questions/62601754/how-to-change-just-one-item-of-qcombobox-to-editable-by-user