QT使用QStackedWidget实现切页显示

在这里插入图片描述
在qt creator中创建一个QT Widgets Application,具体函数如下:
mainwindow.h如下:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QStackedWidget>
#include <QLabel>
#include <QPalette>
#include <QComboBox>
#include <QVBoxLayout>
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::MainWindow *ui;
    QStackedWidget *m_qStackWidget;
public slots:
    void changeStackWidgetIndex(int index);
};

#endif // MAINWINDOW_H

mainwindow.cpp如下:

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

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //实例化一个控件,作为主界面的中心控件
    QWidget *centralwidget = new QWidget(this);
    //实例化 QStackedWidget对象
    m_qStackWidget = new QStackedWidget(this);
    QLabel *label1 = new QLabel(this);
    QLabel *label2 = new QLabel(this);
    QLabel *label3 = new QLabel(this);
    QPalette pal;//调色板
    pal.setColor(QPalette::Background,QColor(255,0,0));//红色
    label1->setPalette(pal);
    label1->setAutoFillBackground(true);
    label1->setText("label1");
    pal.setColor(QPalette::Background,QColor(0,255,0));//绿色
    label2->setPalette(pal);
    label2->setAutoFillBackground(true);
    label2->setText("label2");
    pal.setColor(QPalette::Background,QColor(0,0,255));//蓝色
    label3->setPalette(pal);
    label3->setAutoFillBackground(true);
    label3->setText("label3");
    //在QStackWidget中添加3个label控件作为3个page
    m_qStackWidget->addWidget(label1);
    m_qStackWidget->addWidget(label2);
    m_qStackWidget->addWidget(label3);
    m_qStackWidget->setCurrentIndex(0);
    //添加QComboBox下拉控件
    QComboBox *combx = new QComboBox(this);
    combx->addItem("label1");
    combx->addItem("label2");
    combx->addItem("label3");
    //添加QVBoxLayout垂直布局,并且该布局应用在中心控件centralwidget上
    QVBoxLayout *vblayout = new QVBoxLayout(centralwidget);
    vblayout->addWidget(combx);//
    vblayout->addWidget(m_qStackWidget);
    //设置界面上中心控件为centralwidget
    this->setCentralWidget(centralwidget);
    //添加combx值变换后的响应函数
    connect(combx, QOverload<int>::of(&QComboBox::currentIndexChanged),
          [=](int index){ m_qStackWidget->setCurrentIndex(index); });//通过Lambda表达式的方式写槽函数
//    connect(combx, SIGNAL(currentIndexChanged(int)), this, SLOT(changeStackWidgetIndex(int)));//通过正常方式写槽函数
}

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

void MainWindow::changeStackWidgetIndex(int index)
{
    m_qStackWidget->setCurrentIndex(index);
}

main.cpp如下:

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}
  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GreenHandBruce

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值