Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to get ComboBox->CurrentIndex() in a StackedWidget?
Forum Update on Monday, May 27th 2025

How to get ComboBox->CurrentIndex() in a StackedWidget?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
6 Posts 3 Posters 885 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.
  • Q Offline
    Q Offline
    qtross
    wrote on last edited by qtross
    #1

    Hello,
    I am learning Qt and I have this problem;
    I would like to get current index number which is stated in a stackedWidget.
    I have one ui class and my project structure is just like that;

    centralWidget
    -stackedWidget                               (main stackedWidget)
    -->Index0
    ---
    -->Index1
    ---StackedWidget_2    (another stackedWidget stated in main one)
    ----->Index0
    --------comboBox   (the comboBox I would like to read currentIndex)
    ----->Index1
    ----->Index2
    -->Index2
    ---
    

    What i have tried so far;

    int number= ui->stackedWidget->widget(0)->comboBox->currentIndex();
    

    which gives me an error as widget(0) has no membernamed "comboBox".

    and this;

    QStackedWidget *stackedWidget = new QStackedWidget;
    stackedWidget->addWidget(new QWidget);     // index 0
    auto* widget = stackedWidget->widget(0);
    if (widget) {
        number=widget->comboBox->currentIndex();
     }
    

    Thanks for any suggestions.

    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Based on your code:

      auto* widget = stackedWidget->widget(0);

      widget is an empty widget nothing more.

      With this modified version:

      QStackedWidget *stackedWidget = new QStackedWidget;
      stackedWidget->addWidget(new QComboBox);     // index 0
      auto* comboBox = qobject_cast<QComboBox *>(stackedWidget->widget(0));
      number = comboBox->currentIndex();
      

      It should be clearer I think.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Q 1 Reply Last reply
      0
      • Q qtross

        Hello,
        I am learning Qt and I have this problem;
        I would like to get current index number which is stated in a stackedWidget.
        I have one ui class and my project structure is just like that;

        centralWidget
        -stackedWidget                               (main stackedWidget)
        -->Index0
        ---
        -->Index1
        ---StackedWidget_2    (another stackedWidget stated in main one)
        ----->Index0
        --------comboBox   (the comboBox I would like to read currentIndex)
        ----->Index1
        ----->Index2
        -->Index2
        ---
        

        What i have tried so far;

        int number= ui->stackedWidget->widget(0)->comboBox->currentIndex();
        

        which gives me an error as widget(0) has no membernamed "comboBox".

        and this;

        QStackedWidget *stackedWidget = new QStackedWidget;
        stackedWidget->addWidget(new QWidget);     // index 0
        auto* widget = stackedWidget->widget(0);
        if (widget) {
            number=widget->comboBox->currentIndex();
         }
        

        Thanks for any suggestions.

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

        @qtross

        I would like to get current index number which is stated in a stackedWidget.

        What does this, and your code attempts, mean? Have you (somewhere) created some combobox and put it on a page/widget which is to be shown in the stacked widget? If not, there is no combobox anywhere. Are you talking about which page/widget is currently active in the stacked widget, which is not shown anywhere and has nothing to do with a combobox? Or what?

        EDIT My reply has crossed with @SGaist's. As usual, he may have done a better job at guessing your intention than I! :)

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Based on your code:

          auto* widget = stackedWidget->widget(0);

          widget is an empty widget nothing more.

          With this modified version:

          QStackedWidget *stackedWidget = new QStackedWidget;
          stackedWidget->addWidget(new QComboBox);     // index 0
          auto* comboBox = qobject_cast<QComboBox *>(stackedWidget->widget(0));
          number = comboBox->currentIndex();
          

          It should be clearer I think.

          Q Offline
          Q Offline
          qtross
          wrote on last edited by qtross
          #4

          @SGaist thank you for quick respond!
          I forgot to mention that I already created these; stackedWidget and comboBox in my ui file. I just wanted to reach it's currentindex. However, I neither get any result nor any error with your implementation. I think, I need to mention also this;
          I have one ui class and my project structure is just like that;

          
          centralWidget
          -stackedWidget                               (main stackedWidget)
          -->Index0
          ---
          -->Index1
          ---StackedWidget_2    (another stackedWidget stated in main one)
          ----->Index0
          --------comboBox   (the comboBox I would like to read currentIndex)
          ----->Index1
          ----->Index2
          -->Index2
          ---
          

          @JonB Thank you for your reply also, to be honest it did immidiately when I saw the code he wrote:)
          I did not mention all about these because I thought that I made a mistake in this code line and solution would be simpler than this :/
          int number= ui->stackedWidget_2->Index(0)->comboBox->currentIndex();

          I will edit the main question properly, when the answer is enough to solve the problem. Thanks again.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            So in fact you have have a QComboBox which is inside a QStackedWidget which is itself inside a QStackedWidget.

            So it's going to be:

            ui->comboBox->currentIndex();
            

            Yes, it's unrelated but since you are using designer each widget like the QComboBox is directly accessible by its "name" through the ui object.

            If you want to go through all the steps because you constructed the whole stuff, then you'll have to add one step that first extracts the secondary QStackedWidget from the first one and then the combobox from that one.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            Q 1 Reply Last reply
            2
            • SGaistS SGaist

              So in fact you have have a QComboBox which is inside a QStackedWidget which is itself inside a QStackedWidget.

              So it's going to be:

              ui->comboBox->currentIndex();
              

              Yes, it's unrelated but since you are using designer each widget like the QComboBox is directly accessible by its "name" through the ui object.

              If you want to go through all the steps because you constructed the whole stuff, then you'll have to add one step that first extracts the secondary QStackedWidget from the first one and then the combobox from that one.

              Q Offline
              Q Offline
              qtross
              wrote on last edited by
              #6

              @SGaist wow I did not expect to reach from directly ui form.
              It works great now thank you for your replies!

              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