【发布时间】:2020-06-26 19:38:34
【问题描述】:
我的QCombobox 中有 4 件商品,
'Bryce king'
'James White'
'Russo W'
'Custom Manager'
所以,当我点击"Custom Manager" 时,它应该会变为可编辑,我必须能够输入自己想要的名称。
我已尝试使用 QtCreator 实现此行为,并且在属性中,我可以将其设置为可编辑,但这会使所有项目都可编辑,而不仅仅是一个。
【问题讨论】:
我的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);
}
【讨论】: