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. QComboBox::setCurrentIndex doesn't work when called on constructor
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 4 Posters 1.8k Views
  • 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.
  • H Offline
    H Offline
    hbatalha
    wrote on last edited by hbatalha
    #1

    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!

    JonBJ artwawA 2 Replies Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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() ?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      H 1 Reply Last reply
      3
      • H hbatalha

        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!

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #3

        @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...?

        1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          @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() ?

          H Offline
          H Offline
          hbatalha
          wrote on last edited by
          #4

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

          JonBJ 1 Reply Last reply
          0
          • H hbatalha

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

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #5

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

            H 1 Reply Last reply
            0
            • JonBJ JonB

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

              H Offline
              H Offline
              hbatalha
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

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

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                H 1 Reply Last reply
                0
                • H hbatalha

                  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!

                  artwawA Offline
                  artwawA Offline
                  artwaw
                  wrote on last edited by
                  #8

                  @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();

                  For more information please re-read.

                  Kind Regards,
                  Artur

                  H JonBJ 2 Replies Last reply
                  1
                  • artwawA artwaw

                    @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();

                    H Offline
                    H Offline
                    hbatalha
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

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

                      H Offline
                      H Offline
                      hbatalha
                      wrote on last edited by
                      #10

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

                      Christian EhrlicherC 1 Reply Last reply
                      0
                      • artwawA artwaw

                        @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();

                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on last edited by JonB
                        #11

                        @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!]

                        artwawA 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @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!]

                          artwawA Offline
                          artwawA Offline
                          artwaw
                          wrote on last edited by
                          #12

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

                          For more information please re-read.

                          Kind Regards,
                          Artur

                          1 Reply Last reply
                          1
                          • H hbatalha

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

                            Christian EhrlicherC Online
                            Christian EhrlicherC Online
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

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

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            H 1 Reply Last reply
                            0
                            • Christian EhrlicherC Christian Ehrlicher

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

                              H Offline
                              H Offline
                              hbatalha
                              wrote on last edited by
                              #14

                              @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"?

                              1 Reply Last reply
                              0

                              • Login

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