【问题标题】:How to change just one item of QCombobox to editable by user如何仅将 QCombobox 的一项更改为可由用户编辑
【发布时间】:2020-06-26 19:38:34
【问题描述】:

我的QCombobox 中有 4 件商品,

'Bryce king'
'James White'
'Russo W'
'Custom Manager'

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

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

【问题讨论】:

    标签: c++ qt qcombobox


    【解决方案1】:

    首先确保您的组合框的可编辑属性设置为 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-05
      • 2017-11-14
      • 1970-01-01
      • 1970-01-01
      • 2017-01-07
      • 2018-02-27
      • 2019-06-12
      相关资源
      最近更新 更多