[Solved] Using QStackedWidget to create multiple layouts on one screen
-
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_OBJECTpublic:
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
-
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 ?
-
Thanks SGaist. This worked perfectly and is much neater code.