Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QComboBox::setCurrentIndex doesn't work when called on constructor



  • I have a QComboBox that on startup it has to have its index set to a value read from QSettings.

    The box is declared in the class and is initialized, has its items added, then added to a gridlayout that is then added to the toolbar, all in a function that is called in the constructor right after the call to setupUi.

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this); 
    
        foo();
    
       qDebug() << myBox->currentIndex(); // always 0(zero)
     
       
    ...
    }
    
    void MainWindow::foo()
    {
        myBox = new QComboBox;
    
        myBox->addItem(tr("Item 1"));
        myBox->addItem(tr("Item 2"));
    
        int saved_index = qsettings.value("box_index", "0").toInt(); // this returns value different than 0(zero)
        myBox->setCurrentIndex(saved_index ); // but the this doesn't change nothing 
    
       qDebug() << myBox->currentIndex(); // here the value displayed will be the same as the saved_index but after the Ui is fully loaded and shown it is 0, always 0(zero)
    
        QGridLayout* g_layout = new QGridLayout;
    
        g_layout->addWidget(myBox, 0, 0);
    
        QWidget*  format_widget = new QWidget;
    
        format_widget->setLayout(g_layout);
    
        ui->toolBar->addWidget(format_widget);
    }
    

    I guessing that since the function is called from the constructor the Ui isn't yet fully loaded sp the setCurrentIndex has not effect but I am not sure and found nothing about such in the docs.

    Any help is appreciated! Thanks in adavance!


  • Lifetime Qt Champion

    @hbatalha said in QComboBox::setCurrentIndex doesn't work when called on constructor:

    guessing that since the function is called from the constructor

    You're aware that you create the combobox after you try to call myBox->currentIndex() ?



  • @hbatalha
    As @Christian-Ehrlicher has said. Even more worrying is that what you have as shown does not "crash", which is what I would have expected, so I wonder what you have actually initialized this->myBox to in the first place...?



  • @Christian-Ehrlicher @JonB

    You're aware that you create the combobox after you try to call myBox->currentIndex() ?

    Sorry I miswrote the code order. It's called after foo(); I will edit the post.



  • @hbatalha
    If you do not paste what is actually in your code, nobody can help, and it wastes the time of the people trying to answer...... I'm out.



  • @JonB That is the actual code, I only removed the code that is not part of problem as it would be too long, the only difference is in the variable and function names. Which I changed because I once read that it is best to give neutral names as it prevents people from getting distracted. So I did so ever since. But now I am rethinking it. I can post code with the actual names I have in my code if it does make a difference.


  • Lifetime Qt Champion

    Please provide a minimal compilable example - shouldn't be so hard.



  • @hbatalha
    in line int saved_index = qsettings.value("box_index", "0").toInt(); there is no surprise that it returns non-zero code.
    Slow down and see what you did:

    • you take QVariant from settings named "box_index"
    • you cast it to int
    • but at the same time you supply default value (used in case "box_index" does not exist) as string.

    So, effectively, you cast string to int.

    Line should be int saved_index = qsettings.value("box_index", 0).toInt();



  • @artwaw you are right about that. But doesn't it get the same result, as "0" will be casted to int.



  • @Christian-Ehrlicher The strangest thing just happened

    So I went and created another project for testing so I could post the minimal compilable example.
    I did that but everything worked as expected. I found it strange so I went to the project I am having issues and copied the entire code from the function foo(), (the actual name is populateToolBar, I used the function to add widgets such as comboboxes, labels and buttons to the toolBar) including all the declarations made in the MainWindow class, the headers, everything that has to do with populating the toolbar and it worked as expected in the example project.

    I went to the project having the issue with setCurrentIndex and removed all the code from the constructor except the setupUi and the foo ( actual name: populateToolBar) function so it is the same as the example project and surprisingly IT WORKED, the setCurrentIndex function worked like it should.

    So I undid everything, the code is now the exact one I had when I posted the issue, not a single whitespace added or removed and it is working like it should.

    I just can't explain what happened or why it happened.



  • @artwaw said in QComboBox::setCurrentIndex doesn't work when called on constructor:

    you cast it to int

    but at the same time you supply default value (used in case "box_index" does not exist) as string.

    Just for the record, I do not know what you mean here.

    int saved_index = qsettings.value("box_index", "0").toInt();
    

    Should work fine. This is because QVariant("0").toInt() will convert a QString to an int. Remember that the default "0" is an argument to QSettings::value(), before the toInt() is applied. It will have the same effect as writing int saved_index = qsettings.value("box_index", 0).toInt();. Though I prefer your way of supplying the default as an int, there is nothing wrong with the string way. [Untested by me, but I imagine so!]



  • @JonB Might be I am mistaken then, somehow I thought that result might be not reliable like you described. My bad.


  • Lifetime Qt Champion

    @hbatalha said in QComboBox::setCurrentIndex doesn't work when called on constructor:

    So I undid everything, the code is now the exact one I had when I posted the issue,

    I would guess it's not the same but when you say so... that's a good reason to use a cvs.



  • @Christian-Ehrlicher said in QComboBox::setCurrentIndex doesn't work when called on constructor:

    I would guess it's not the same but when you say so

    I am 101% sure, the code in MainWindow was exactly the same.

    By "cvs" do you mean "version control"?


Log in to reply