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

How to use QStackedWidget



  • 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);
        }

  • Lifetime Qt Champion

    @Inzinejkr said in How to use QStackedWidget:

    but it's crashing the application.

    Use a debugger to see where it actually crashes.



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


  • Lifetime Qt Champion

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



  • 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?



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



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



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


Log in to reply