How to use QStackedWidget in GUI?

2019-07-13 03:21发布

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?

1条回答
戒情不戒烟
2楼-- · 2019-07-13 04:20

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

Your are new to Qt so i suggest you to use Qt Designer: Image Example

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

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

ui->stackedWidget->setCurrentIndex(1);
查看更多
登录 后发表回答