简体   繁体   English

如何仅将 QCombobox 的一项更改为用户可编辑

[英]How to change just one item of QCombobox to editable by user

I have 4 items in my QCombobox ,我的QCombobox中有 4 个项目,

'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.因此,当我单击"Custom Manager"时,它应该变为可编辑,并且我必须能够输入自己想要的名称。

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.我尝试使用 QtCreator 来实现此行为,并且在属性中,我可以将其设置为可编辑,但这会使所有项目都可编辑,而不仅仅是一个。

First make sure your combobox's editable property is set to false.首先确保您的组合框的可编辑属性设置为 false。

Then you can use theQComboBox::currentIndexChanged signal and check the value of the current index and then make the combobox editable.然后您可以使用QComboBox::currentIndexChanged信号并检查当前索引的值,然后使 combobox 可编辑。 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:此外,如果要将值保存到已编辑的文本中,请使用QComboBox::currentTextChanged信号:

void MainWindow::on_comboBox_currentTextChanged(const QString &arg1)
{
    qDebug() << arg1;
    ui->comboBox->setItemText(ui->comboBox->currentIndex(), arg1);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM