[英]Editable QComboBox: synchronize edit text with item text
I've got a QComboBox
which I want to be "automatically" editable. 我有一个QComboBox
,我希望它可以“自动”编辑。 That is, every time a user manually changes current item's text, that text should "fall" to the underlying model automatically. 也就是说,每次用户手动更改当前项目的文本时,该文本应自动“落入”基础模型。
So far, I've reached this via a custom signal handler: 到目前为止,我已经通过自定义信号处理程序达到了这一点:
void setupUi() {
...
connect( someComboBox,
SIGNAL(editTextChanged(QString)),
SLOT(comboBoxEditTextChanged(QString)) );
...
}
void comboBoxEditTextChanged( const QString& text ) {
someComboBox->setItemText( someComboBox->currentIndex(), text );
}
So I wonder, is there a possibility to do this with less code? 所以我想知道,是否有可能用更少的代码来做到这一点? I've tried QComboBox::setInsertPolicy(QComboBox::InsertAtCurrent)
, but that didn't help. 我已经尝试过QComboBox::setInsertPolicy(QComboBox::InsertAtCurrent)
,但这没有帮助。
EDIT: Current method with a custom slot works properly - but I'm asking if there's a method that does not involve any signals/slots. 编辑:具有自定义插槽的当前方法可以正常工作-但我问是否有一种方法不涉及任何信号/插槽。
To set the Text Automatically when USER changes it, we can edit your slot as follows: 要在用户更改文本时自动设置文本,我们可以按以下方式编辑您的广告位:
void comboBoxEditTextChanged( const QString& text )
{
int index = someComboBox->findText(text);
if(index != -1)
{
someComboBox->setCurrentIndex(index);
}
someComboBox->setItemText( someComboBox->currentIndex(), text );
}
I hope this will resolve your issue 我希望这能解决您的问题
QComboBox can add items manually using QComboBox可以使用手动添加项目
combo->additem("X");
combo->addItem(QString Y);
whereas you can manage the maximum number of items in it. 而您可以管理其中的最大项目数。 Please go through the following link for details. 请通过以下链接获取详细信息。
So, in your slot, 因此,在您的广告位中
void comboBoxEditTextChanged( const QString& text )
{
someComboBox->addItem(text);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.