#include "MainWindow.h"
#include "ui_MainWindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
pwind = new QWidget;
setCentralWidget(pwind);
QPushButton *btnT1= new QPushButton("btn1");
QPushButton *btnT2= new QPushButton("btn2");
QPushButton *btnT3= new QPushButton("btn3");
QLabel *pttt1= new QLabel(u8"----->",this);
QLabel *pttt2= new QLabel(u8"----->",this);
m_pStackedWidget = new QStackedWidget();
btnT4 = new QPushButton(u8"子页面1");
btnT5 = new QPushButton(u8"子页面2");
btnT6 = new QPushButton(u8"子页面3");
m_pStackedWidget->addWidget(btnT4);
m_pStackedWidget->addWidget(btnT5);
m_pStackedWidget->addWidget(btnT6);
QGridLayout *pLayout = new QGridLayout();
pLayout->addWidget(btnT1,0,0);
pLayout->addWidget(pttt1,0,1);
pLayout->addWidget(btnT2,0,2);
pLayout->addWidget(pttt2,0,3);
pLayout->addWidget(btnT3,0,4);
pLayout->addWidget(m_pStackedWidget,1,0,2,5); //addWidget(待添加QWidget,行数,列数,跨越行数,跨越列数);后面的参数可以缺省
pwind->setLayout(pLayout);
connect(btnT1, &QPushButton::clicked, this, &MainWindow::sss1);
connect(btnT2, &QPushButton::clicked, this, &MainWindow::sss2);
connect(btnT3, &QPushButton::clicked, this, &MainWindow::sss3);//通过控制sss1、sss2、sss3的参数变化,来实现对switchPage(int nIndex)的控制
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::sss1()//设定显示页面1
{
int i = 0;
switchPage(i);//将要显示的页面编号传递给switchPage(int nIndex);
}
void MainWindow::sss2()//设定显示页面2
{
int i = 1;
switchPage(i);
}
void MainWindow::sss3()//设定显示页面3
{
int i = 2;
switchPage(i);
}
void MainWindow::switchPage(int nIndex)
{
m_pStackedWidget->setCurrentIndex(nIndex);
}