在UI设计师中添加StackedWidget控件,如下图:
在窗口中添加两个按钮,分别命名为NextButton和PrevousButton。然后右键点击转到槽,分别把槽函数实现为下面的形式:
void MainWindow::on_PreviousButton_clicked()//上一个按钮对应的槽函数
{
int ncount = this->ui->stackedWidget->count();//求出StackedWidget的总页面数
int current = this->ui->stackedWidget->currentIndex(); //求出当前页面的索引(此处的索引范围类似于数组下标,都是0到n-1)
current = (current-1+ncount) % ncount;//循环显示
this->ui->stackedWidget->setCurrentIndex(current);
this->ui->stackedWidget->update(); //更新stackWidget
// this->ui->stackedWidget->show(); //show函数也可以实现更新界面
}
void MainWindow::on_NextButton_clicked() //下一个按钮对应的槽函数
{
int ncount = this->ui->stackedWidget->count();
int current = this->ui->stackedWidget->currentIndex();
current = (current+1+ncount) % ncount;
this->ui->stackedWidget->setCurrentIndex(current);
this->ui->stackedWidget->update();
}
分别对StackedWidget中的页面进行不同的设置用以进行区分对比。StackedWidget默认是两页,觉着不够可以鼠标右键点击插入页。
另外要注意QT中的MainWindow和Ui::MainWindow,这两货到底是不是同一个类,如果不是,它俩到底分别指的是什么玩意儿。目前我也不知道准确的答案,哪位大佬若知道准确答案欢迎 指教,鄙人感激不尽。