Reputation: 2734
I've got a QComboBox
which I want to be "automatically" editable. 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.
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.
Upvotes: 2
Views: 9694
Reputation: 948
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
Upvotes: 2
Reputation: 948
QComboBox can add items manually using
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);
}
Upvotes: -1