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. [Solved] Using QStackedWidget to create multiple layouts on one screen
Forum Update on Monday, May 27th 2025

[Solved] Using QStackedWidget to create multiple layouts on one screen

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 3.0k 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.
  • T Offline
    T Offline
    TimAnderson
    wrote on 7 Aug 2014, 21:05 last edited by
    #1

    Hi,

    I am trying to create program with a few pushbuttons that will always stay active on the right hand side of the screen and when the user clicks those buttons different items will show up on the left part of the screen. My "previous attempt created a memory leak":http://qt-project.org/forums/viewthread/45979/#188166 and I was pointed towards QStackedWidget or QStackedLayout. I have implemented this and it works, no memory leak.

    Still, this seems like a really long winded way around this problem. I essentially need either a QStackedWidget or a QStackedLayout for each cell in my main QGridLayout since it doesn't appear like QStackedLayout will accept a layout, only widgets.

    Others must have done something similar. This seems like a very basic thing but it seems like most examples I see online solve the problem by opening a new window which I really want to avoid if possible. Are people writing their own version of QStackedLayout to accept a layout? Is there some other key feature I am missing? Or is this really the best solution to the problem. The end product here is going to involve many more cells than this and it seems like using a sledgehammer to pound in a nail. Anyway, here is the code:

    Header:
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QPushButton>
    #include <QComboBox>
    #include <QLineEdit>
    #include <QLabel>
    #include <QGridLayout>
    #include <QStackedWidget>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    QPushButton *ButtonA;
    QPushButton *ButtonB;
    
    QComboBox *Combo[3];
    QLineEdit *LineEdit[3];
    QLabel *Label[3];
    
    QGridLayout *MainLayout;
    QStackedWidget *StackedWidget[3][2];
    

    private slots:

    void on_ButtonAclick();
    void on_ButtonBclick();
    

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H@

    Main:
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    ButtonA = new QPushButton("A");
    ButtonB = new QPushButton("B");
    
    connect(ButtonA,SIGNAL(clicked()),this,SLOT(on_ButtonAclick()));
    connect(ButtonB,SIGNAL(clicked()),this,SLOT(on_ButtonBclick()));\
    
    MainLayout = new QGridLayout;
    
    Combo[0] = new QComboBox();
    Combo[0]->addItem("Top Box");
    Combo[1] = new QComboBox();
    Combo[1]->addItem("Middle Box");
    Combo[2] = new QComboBox();
    Combo[2]->addItem("Bottom Box");
    
    Label[0] = new QLabel("Top");
    Label[1] = new QLabel("Middle");
    Label[2] = new QLabel("Bottom");
    
    LineEdit[0] = new QLineEdit;
    LineEdit[1] = new QLineEdit;
    LineEdit[2] = new QLineEdit;
    
    for(int i=0;i<3;i++)
        for(int j=0;j<2;j++)
            StackedWidget[i][j] = new QStackedWidget;
    
    StackedWidget[0][0]->addWidget(new QLabel("Welcome"));
    StackedWidget[1][0]->addWidget(new QLabel(""));
    StackedWidget[2][0]->addWidget(new QLabel(""));
    StackedWidget[0][1]->addWidget(new QLabel(""));
    StackedWidget[1][1]->addWidget(new QLabel(""));
    StackedWidget[2][1]->addWidget(new QLabel(""));
    
    for(int i=0;i<3;i++)
    {
        StackedWidget[i][0]->addWidget(Label[i]);
        StackedWidget[i][1]->addWidget(LineEdit[i]);
    }
    
    for(int i=0;i<3;i++)
        StackedWidget[i][0]->addWidget(Combo[i]);
    
    MainLayout->addWidget(ButtonA,0,2);
    MainLayout->addWidget(ButtonB,1,2);
    for(int i=0;i<3;i++)
        for(int j=0;j<2;j++)
            MainLayout->addWidget(StackedWidget[i][j],i,j);
    
    ui->centralWidget->setLayout(MainLayout);
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_ButtonAclick()
    {
    for(int i=0;i<3;i++)
    for(int j=0;j<2;j++)
    StackedWidget[i][j]->setCurrentIndex(1);
    }

    void MainWindow::on_ButtonBclick()
    {
    for(int i=0;i<3;i++)
    {
    StackedWidget[i][0]->setCurrentIndex(2);
    StackedWidget[i][1]->setCurrentIndex(0);
    }
    }
    @

    Thanks for your time,

    Tim

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on 7 Aug 2014, 21:35 last edited by
      #2

      It will be easier to help you with a picture of your UI design.
      Could you post photoshoped or napkin UI.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on 7 Aug 2014, 22:05 last edited by
        #3

        Hi,

        Why not create a custom widget containing one QComboBox, QLineEdit and QLabel that you can configure with the text you want. and make three instance of it in your MainWindow ?

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

        1 Reply Last reply
        0
        • T Offline
          T Offline
          TimAnderson
          wrote on 8 Aug 2014, 04:24 last edited by
          #4

          Thanks SGaist. This worked perfectly and is much neater code.

          1 Reply Last reply
          0

          1/4

          7 Aug 2014, 21:05

          • Login

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