如何在 GUI 中使用 QStackedWidget? [英] How to use QStackedWidget in GUI?

查看:40
本文介绍了如何在 GUI 中使用 QStackedWidget?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Qt 的新手,我必须为此制作一个具有多个窗口的 GUI,我使用 Qt 设计器工具找到了 QStackedWidget 类.

我添加了 QStackedWidget 使用 add new->Qt 设计器表单类->Qstackwidget

之后我在主窗口中创建了这个类的对象

#ifndef MAINWINDOW_H#define MAINWINDOW_H#include #include命名空间 Ui { class MainWindow;}类 MainWindow : 公共 QMainWindow {Q_OBJECT上市:显式主窗口(QWidget *parent = 0);~主窗口();私人插槽:void on_pushButton_clicked();私人的:用户界面::主窗口 *用户界面;StackedWidget *stk;};#endif//MAINWINDOW_H

然后我尝试通过以下方式显示 StackedWidget:

#include "mainwindow.h"#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :QMainWindow(父),用户界面(新用户界面::主窗口){ui->setupUi(this);}主窗口::~主窗口(){删除用户界面;}void MainWindow::on_pushButton_clicked(){stk = new StackedWidget(this);stk->show();}

但是 stackwidget 没有打开.

谁能告诉我我做错了什么以及如何使用设计器工具实现 QStackedWidget GUI?

解决方案

您可以将 StackedWidget 拖放到您的表单中,对其进行自定义,然后使用箭头转到下一页并进行处理.

StackedWidget 就像一个向量,您可以通过索引访问它们.

ui->stackedWidget->setCurrentIndex(1);

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

这篇关于如何在 GUI 中使用 QStackedWidget?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆