QStackedWidget问题
传统的QStackedWidget创建的界面是和QListWidget一起使用的,但是我想创建成一组按钮,点击每个按钮能分别的对应QStackedWidget里的子界面,我编写的代码如下:
mainwindow.h
class MainWindow : public QWidget , public Ui::mainWindow{ Q_OBJECTpublic: MainWindow(QWidget *parent=0); ~MainWindow(void);protected:private slots: void changed_1(); //定义setCurrentIndex接收槽 void changed_2();private: //创建切换页面 void creatTest_1Page(); void creatTest_2Page(); QStackedLayout *stackedLayout; QWidget *test_1Page; QWidget *test_2Page; QStackedWidget *stackedWidget;};
MainWindow::MainWindow(QWidget *parent) : QWidget(parent){ setupUi(this); //分页面窗口的实现 void creatTest_1Page(); void creatTest_2Page(); QStackedWidget *stackedWidget=new QStackedWidget; stackedWidget->addWidget(test_1Page); stackedWidget->addWidget(test_2Page); connect(testButton_1,SIGNAL(clicked()),this,SLOT(changed_1())); connect(testButton_2,SIGNAL(clicked()),this,SLOT(changed_2()));//窗口布局管理 QHBoxLayout *middleButtonLayout=new QHBoxLayout; middleButtonLayout->addWidget(testButton_1); middleButtonLayout->addWidget(testButton_2); QVBoxLayout *mainLayout=new QVBoxLayout; mainLayout->addWidget(topLabel); mainLayout->addLayout(middleButtonLayout); mainLayout->addWidget(line); mainLayout->addWidget(stackedWidget); setLayout(mainLayout);}MainWindow::~MainWindow(){ mainWindowP->close();}void MainWindow::closeWindow(){ mainWindowP->close(); close();}void MainWindow::changed_1(){ stackedWidget->setCurrentIndex(0);}void MainWindow::changed_2(){ stackedWidget->setCurrentIndex(1);}void MainWindow::creatTest_1Page(){ test_1Page = new QWidget;}void MainWindow::creatTest_2Page(){ test_2Page = new QWidget;}