Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QComboBox not updating index when selection changed
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QComboBox not updating index when selection changed

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 3.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nakedmind
    wrote on 18 Sept 2014, 16:13 last edited by
    #1

    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.
    Thanks

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 18 Sept 2014, 16:20 last edited by
      #2

      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

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nakedmind
        wrote on 18 Sept 2014, 16:59 last edited by
        #3

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

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

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

        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);
        thicknessLabel2->setBuddy(thicknessInput2);

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

        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);
        setLayout(mainLayout);

        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;
        resultBox->setText(tr(ss.str().c_str()));
        resultBox->exec();
        return;
        }@

        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.
        Thanks

        1 Reply Last reply
        0
        • N Offline
          N Offline
          nakedmind
          wrote on 18 Sept 2014, 17:23 last edited by
          #4

          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.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dheerendra
            Qt Champions 2022
            wrote on 18 Sept 2014, 17:30 last edited by
            #5

            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)) {@

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            1 Reply Last reply
            0
            • N Offline
              N Offline
              nakedmind
              wrote on 20 Sept 2014, 00:51 last edited by
              #6

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

              1 Reply Last reply
              0

              1/6

              18 Sept 2014, 16:13

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved