QComboBox confusion
-
I am trying to implement a zoom select with QComboBox. Lets assume there are the following items in the combo box:
25% -> should lead to a signal setZoom(25)
50% -> should lead to a signal setZoom(50)
100% -> should lead to a signal setZoom(100)So far, so good. Now comes the challenge: The combo should be editable. When the user types 80 <ENTER>, a signal setZoom(80) should be emitted and the text in the combo should be set to "80%", but NO new item should be added to the drop down menu.
I have fiddled with different events of QComboBox and the QLineEdit inside but I can't see how to do this. Any hints?
Cheers,
Eddy
-
Hi,
Did you try setting "insertPolicy":http://qt-project.org/doc/qt-4.8/qcombobox.html#insertPolicy-prop to QComboBox::NoInsert ?
-
Something like this should work:
@
ui->comboBox->setEditable(true);
ui->comboBox->setInsertPolicy(QComboBox::NoInsert);connect(ui->comboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
[=](int index){
emit setZoom(getZoomFromText(ui->comboBox->itemText(index)));
});connect(ui->comboBox->lineEdit(), &QLineEdit::returnPressed,
={
emit setZoom(getZoomFromText(ui->comboBox->lineEdit()->text()));
});
@Edit ups, too slow :)