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

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

我的 QCombobox

中有 4 件商品
'Bryce king'
'James White'
'Russo W'
'Custom Manager'

因此,当我点击 "Custom Manager" 时,它应该变为可编辑状态,我必须能够输入我自己想要的名称。

我已尝试使用 QtCreator 实现此行为,并且在属性中,我可以将其设置为可编辑,但这会使所有项目都可编辑,而不仅仅是一个。

首先确保您的组合框的可编辑 属性 设置为 false。

然后您可以使用QComboBox::currentIndexChanged 信号并检查当前索引的值,然后使组合框可编辑。示例:

void MainWindow::on_comboBox_currentIndexChanged(const QString &arg1)
{
    if (arg1 == "Custom Manager") {
        ui->comboBox->setEditable(true);
    }
    else {
        ui->comboBox->setEditable(false);
    }
}

此外,如果您想将值保存到编辑的文本中,请使用 QComboBox::currentTextChanged 信号:

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