Important: Please read the Qt Code of Conduct -

[SOLVED] QComboBox not updating index when selection changed

  • I'm putting together a simple application to do a calculation for a university project. It's got a few (4) input fields (QLineEdit) and a few (2) selection lists (QComboBox) so the user can select the input parameters to use in the calculation. This is arranged in a QGridLayout.

    I have a QPushButton at the bottom to run the calculation; the clicked() signal is connected to a function to error check the inputs to make sure they're within the ranges defined by the assignment. I'm having an issue where the QComboBox indices don't seem to be updating consistently when I change the selections. I will, for example, set the second combo box to the 3rd index and it will update properly; however if I set it back to the 0th index, it won't update at all and currentIndex() continues to return 3.

    It seems to be an issue with the 0th index on the second combo box, because when I change it to another index (starts at 0 by default), then change it back to 0, neither of my combo box indices update until I change the second one to a non-0 index. As long as the second combo box is at a non-0 index, both combo box indices update normally. Obviously this is an issue because if you run the calculation, then decide to change the parameters and want the 0th index of the second combo box, you have to restart the program.

    What am I missing here? Both combo boxes are constructed the same way.

  • Qt Champions 2017

    Your sample simple sample code snippet would help us to help you. I have not faced such issue. Since you are facing code snippet makes it better

  • Here is a snippet of the main widget constructor:
    @ particleInput = new QLineEdit(this);
    particleLabel = new QLabel(tr("Incoming &particle Z:"), this);

    matComboBox1 = new QComboBox(this);
    matComboBox1->addItem(tr("Z=1 (hydrogen)"));
    ...bunch of entries...
    matLabel1 = new QLabel(tr("Target material &1:"), this);

    thicknessInput1 = new QLineEdit(this);
    thicknessLabel1 = new QLabel(tr("Material 1 &thickness (cm):"), this);

    matComboBox2 = new QComboBox(this);
    matComboBox2->addItem(tr("No target"));
    matComboBox2->addItem(tr("Z=1 (hydrogen)"));
    ...bunch of entries...

    thicknessInput2 = new QLineEdit(this);
    thicknessLabel2 = new QLabel(tr("Material 2 t&hickness (cm):"), this);

    energyInput = new QLineEdit(this);
    energyLabel = new QLabel(tr("Particle &energy/nucleon (MeV):"), this);

    calcButton = new QPushButton(tr("&Calculate"), this);

    resultBox = new QMessageBox(this);

    QGridLayout *mainLayout = new QGridLayout(this);
    mainLayout->addWidget(particleLabel, 0, 0, 1, 1);
    mainLayout->addWidget(particleInput, 0, 1, 1, 1);
    mainLayout->addWidget(energyLabel, 1, 0, 1, 1);
    mainLayout->addWidget(energyInput, 1, 1, 1, 1);
    mainLayout->addWidget(matLabel1, 2, 0, 1, 1);
    mainLayout->addWidget(matComboBox1, 2, 1, 1, 1);
    mainLayout->addWidget(thicknessLabel1, 3, 0, 1, 1);
    mainLayout->addWidget(thicknessInput1, 3, 1, 1, 1);
    mainLayout->addWidget(matLabel2, 4, 0, 1, 1);
    mainLayout->addWidget(matComboBox2, 4, 1, 1, 1);
    mainLayout->addWidget(thicknessLabel2, 5, 0, 1, 1);
    mainLayout->addWidget(thicknessInput2, 5, 1, 1, 1);
    mainLayout->addWidget(calcButton, 6, 0, 1, 2);

    connect(calcButton, SIGNAL(clicked()), this, SLOT(checkInputs()));@

    Here is the relevant part of the checkInputs() function:
    @ double t2 = thicknessInput2->text().toDouble();
    int imat1 = matComboBox1->currentIndex();
    int imat2 = matComboBox2->currentIndex();

    if ((imat2 != 0) && (t2 == 0)) {
    ss << "Please set the thickness of the second material, ";
    ss << "or set the second material to "No target": imat1=" << imat1;
    ss << ", imat2=" << imat2;

    Again, the problem is that both combo box indices won't update once, through the UI, I change the second combo box to a non-0 index, then change it back to 0. In other words, that if statement in checkInputs() will always evaluate to true once I change the second combo box input to anything other than "No target", even afte r I change it back.

  • After some more testing, I've determined that the QMessageBox is blocking the UI from updating even after I dismiss it because I was calling exec(). I'm not sure why this is the case as from what I can tell from the documentation, it should stop blocking after I close it. However, using show() instead resolves the issue.

  • Qt Champions 2017

    It updates properly. I tried with 2 combo boxes.

    Is your check correct ? You are condition is based on one combo box and one line edit ?

    @if ((imat2 != 0) && (t2 == 0)) @

    Why are you checking t2 rather than imat2 ?

    I feel it should have

    @if ((imat2 != 0) && (imat2 == 0)) {@

  • Yes the check is correct, the program works correctly when I change exec() to show().

Log in to reply