简体   繁体   中英

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?

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: 图片范例

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM