Important: Please read the Qt Code of Conduct -

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?



  • Moderators


    Did you try setting "insertPolicy": to QComboBox::NoInsert ?

  • Moderators

    Something like this should work:

    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 :)

Log in to reply