vasu gupta
vasu gupta

Reputation: 315

How to use QStackedWidget in GUI?

I am new to Qt and am have to make a GUI having multiple windows for this I found QStackedWidget class using Qt designer tools.

I added QStackedWidget using add new->Qt designer form class->Qstackwidget

after that I created an object of this class in my main window

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<stackedwidget.h>

namespace Ui { class MainWindow; }

class MainWindow : public QMainWindow {
    Q_OBJECT

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

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
    StackedWidget *stk; };

#endif // MAINWINDOW_H

then i tried to display StackedWidget by:

#include "mainwindow.h"
#include "ui_mainwindow.h"

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

}

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

void MainWindow::on_pushButton_clicked()
{
    stk = new StackedWidget(this);
    stk->show();
}

But stackwidget is not opening .

Can someone tell me what am I doing wrong and how to implement QStackedWidget GUI using designer tools?

Upvotes: 7

Views: 27599

Answers (1)

Farhad
Farhad

Reputation: 4181

The QStackedWidget class provides a stack of widgets where only one widget is visible at a time.

You are new to Qt so I suggest you to using Qt Designer: Image Example

You can drag&drop StackedWidget to your form, customize it then use arrows to go to the next page and work on it too.

StackedWidget is like a vector you can access them via indexes.

ui->stackedWidget->setCurrentIndex(1);

Upvotes: 13

Related Questions