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. How to use QStackedWidget

How to use QStackedWidget

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 2.1k 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.
  • I Offline
    I Offline
    Inzinejkr
    wrote on last edited by
    #1

    Okay, so I added a stackedwidget via the designer, now I'd like to edit it via code.
    I'd like to add 5 labels on a few pages of the stacked widget. This is the code I'm trying to use, but it's crashing the application. Can someone help me with this.

    for (int i=0; i<5; i++)
        {
            QHBoxLayout *lay=new QHBoxLayout();
            QWidget *wdg=new QWidget(this);
            for (int j=0; j<5; j++)
            {
                QLabel *lbl=new QLabel();
                lbl->setText("Label: "+QString::number(j));
                lay->addWidget(lbl);
            }
            wdg->setLayout(lay);
            ui->stck->insertWidget(i, wdg);
        }
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Inzinejkr said in How to use QStackedWidget:

      but it's crashing the application.

      Use a debugger to see where it actually crashes.

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

      1 Reply Last reply
      0
      • I Offline
        I Offline
        Inzinejkr
        wrote on last edited by
        #3

        Umm, I tried and it doesn't crash now but it still doesn't show any of the labels.. What am I missing?

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

          Your code above looks fine but don't know what else you're doing.

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

          1 Reply Last reply
          1
          • I Offline
            I Offline
            Inzinejkr
            wrote on last edited by
            #5

            It's just a project to see how it works, so nothing besides the code shown. Do I need to do anything else to actually show the page?

            JonBJ 1 Reply Last reply
            0
            • I Inzinejkr

              It's just a project to see how it works, so nothing besides the code shown. Do I need to do anything else to actually show the page?

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

              @Inzinejkr
              Apart from the fact that we cannot even be sure where you are putting your code.

              At a guess, you are inserting your widgets, but probably leaving the design-time current widget --- which is perhaps blank --- of the QStackedWidget as-is?

              Inserting a new widget at an index less than or equal to the current index will increment the current index, but keep the current widget.

              Have you understood that:

              QStackedWidget provides no intrinsic means for the user to switch page. This is typically done through a QComboBox or a QListWidget that stores the titles of the QStackedWidget's pages. For example:

              Did you mean to perhaps use a QTabWidget ?

              1 Reply Last reply
              0
              • I Offline
                I Offline
                Inzinejkr
                wrote on last edited by
                #7

                @JonB said in How to use QStackedWidget:

                but probably leaving the design-time current widget --- which is perhaps blank --- of the QStackedWidget as-is?

                I can't wrap my head around this sentence, but I fixed it by adding

                ui->stck->setCurrentIndex(0);
                

                after the loop, which is what I'm guessing you said.. Thanks!

                JonBJ 1 Reply Last reply
                1
                • I Inzinejkr

                  @JonB said in How to use QStackedWidget:

                  but probably leaving the design-time current widget --- which is perhaps blank --- of the QStackedWidget as-is?

                  I can't wrap my head around this sentence, but I fixed it by adding

                  ui->stck->setCurrentIndex(0);
                  

                  after the loop, which is what I'm guessing you said.. Thanks!

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

                  @Inzinejkr said in How to use QStackedWidget:

                  I can't wrap my head around this sentence

                  Oh dear, I try to make my answers clear, sorry about that!

                  If you do not go setCurrentIndex(something) then the widget being shown is just whatever it was however you left it in the Designer. Which might be some blank widget if that's what you had there, or possibly "nothing" if QStackedWidget allows that.

                  Either way, what you have now done sets the current widget explicitly from code, so it will indeed be one of the widgets you added into the stack in your code at runtime. Hence fixing your issue.

                  With a QStackedWidget it is your task in code to call setCurrentIndex() whenever you want the current widget displayed to change. A QTabWidget behaves like a QStackedWidget, but has the additional "furniture" of a QTabBar to provide an interface to the user for changing which page/widget is shown.

                  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