How to change just one item of QCombobox to editable by user

半腔热情 提交于 2021-01-29 12:23:01

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!