简体   繁体   English

如何在GUI中使用QStackedWidget?

[英]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. 我是Qt的新手,为此我必须制作一个具有多个窗口的GUI,我使用Qt设计器工具找到了QStackedWidget类。

I added QStackedWidget using add new->Qt designer form class->Qstackwidget 我加QStackedWidget使用添加新建- > Qt设计形式类- > 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: 然后我尝试通过以下方式显示StackedWidget:

#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 . 但是stackwidget没有打开。

Can someone tell me what am I doing wrong and how to implement QStackedWidget GUI using designer tools? 有人可以告诉我我在做什么错,以及如何使用设计器工具实现QStackedWidget GUI吗?

The QStackedWidget class provides a stack of widgets where only one widget is visible at a time. QStackedWidget类提供了一堆小部件,其中一次仅一个小部件可见。

Your are new to Qt so i suggest you to use Qt Designer: 您是Qt的新手,所以我建议您使用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拖放到窗体上,对其进行自定义,然后使用箭头转到下一页并对其进行处理。

StackedWidget like a vector you can access to them via indexes. StackedWidget就像一个向量,您可以通过索引访问它们。

ui->stackedWidget->setCurrentIndex(1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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