QT下界面跳转

转自:https://blog.csdn.net/tj807126663/article/details/33738563

简介

    Qt图形应用中经常会遇到从一个界面跳转到另一个界面应用,这里简单介绍一种使用信号-槽机制实现的界面跳转方案。创建one,two, three三个图像界面类,每一个界面类中实现一个信号函数,在按钮的槽函数中发送该信号, 使用QStackedLayout存放所有的界面,在主窗口使用connect将信号和QStackedLayout::setCurrentIndex关联起来。

界面one

    界面one图形如下:


one.h

[cpp]  view plain  copy
  1. #ifndef ONE_H  
  2. #define ONE_H  
  3.   
  4. #include <QWidget>  
  5.   
  6. class Two;  
  7.   
  8. namespace Ui {  
  9. class One;  
  10. }  
  11.   
  12. class One : public QWidget  
  13. {  
  14.     Q_OBJECT  
  15.   
  16. public:  
  17.     explicit One(QWidget *parent = 0);  
  18.     ~One();  
  19.   
  20. signals:  
  21.     void display(int number);  
  22.   
  23. private slots:  
  24.     void on_nextPushButton_clicked();  
  25.   
  26. private:  
  27.     Ui::One *ui;  
  28.     Two *two;  
  29. };  
  30.   
  31. #endif // ONE_H  

one.cpp

   

[cpp]  view plain  copy
  1. #include "one.h"  
  2. #include "ui_one.h"  
  3. #include "two.h"  
  4. #include "widget.h"  
  5.   
  6. One::One(QWidget *parent) :  
  7.     QWidget(parent),  
  8.     ui(new Ui::One)  
  9. {  
  10.     ui->setupUi(this);  
  11.   
  12. }  
  13.   
  14. One::~One()  
  15. {  
  16.     delete ui;  
  17. }  
  18.   
  19. void One::on_nextPushButton_clicked()  
  20. {  
  21.     emit display(1);  
  22. }  

界面two

    界面two图形如下:

two.h



[cpp]  view plain  copy
  1. #ifndef TWO_H  
  2. #define TWO_H  
  3.   
  4. #include <QWidget>  
  5.   
  6. namespace Ui {  
  7. class Two;  
  8. }  
  9.   
  10. class Two : public QWidget  
  11. {  
  12.     Q_OBJECT  
  13.   
  14. public:  
  15.     explicit Two(QWidget *parent = 0);  
  16.     ~Two();  
  17.   
  18. signals:  
  19.     void display(int number);  
  20.   
  21.   
  22. private slots:  
  23.     void on_previousPushButton_clicked();  
  24.   
  25.     void on_nextPushButton_clicked();  
  26.   
  27. private:  
  28.     Ui::Two *ui;  
  29. };  
  30.   
  31. #endif // TWO_H  

two.cpp

[cpp]  view plain  copy
  1. #include "two.h"  
  2. #include "ui_two.h"  
  3.   
  4. Two::Two(QWidget *parent) :  
  5.     QWidget(parent),  
  6.     ui(new Ui::Two)  
  7. {  
  8.     ui->setupUi(this);  
  9. }  
  10.   
  11. Two::~Two()  
  12. {  
  13.     delete ui;  
  14. }  
  15.   
  16. void Two::on_previousPushButton_clicked()  
  17. {  
  18.     emit display(0);  
  19. }  
  20.   
  21. void Two::on_nextPushButton_clicked()  
  22. {  
  23.     emit display(2);  
  24. }  

界面three

    界面three图形如下:

three.h

[cpp]  view plain  copy
  1. #ifndef THREE_H  
  2. #define THREE_H  
  3.   
  4. #include <QWidget>  
  5.   
  6. namespace Ui {  
  7. class Three;  
  8. }  
  9.   
  10. class Three : public QWidget  
  11. {  
  12.     Q_OBJECT  
  13.   
  14. public:  
  15.     explicit Three(QWidget *parent = 0);  
  16.     ~Three();  
  17.   
  18. signals:  
  19.     void display(int number);  
  20.   
  21. private slots:  
  22.     void on_previousPushButton_clicked();  
  23.   
  24. private:  
  25.     Ui::Three *ui;  
  26. };  
  27.   
  28. #endif // THREE_H  

three.cpp

[cpp]  view plain  copy
  1. #include "three.h"  
  2. #include "ui_three.h"  
  3.   
  4. Three::Three(QWidget *parent) :  
  5.     QWidget(parent),  
  6.     ui(new Ui::Three)  
  7. {  
  8.     ui->setupUi(this);  
  9. }  
  10.   
  11. Three::~Three()  
  12. {  
  13.     delete ui;  
  14. }  
  15.   
  16. void Three::on_previousPushButton_clicked()  
  17. {  
  18.     emit display(1);  
  19. }  

主界面

widget.h

[cpp]  view plain  copy
  1. #ifndef WIDGET_H  
  2. #define WIDGET_H  
  3.   
  4. #include <QWidget>  
  5.   
  6. class One;  
  7. class Two;  
  8. class Three;  
  9. class QStackedLayout;  
  10. class QVBoxLayout;  
  11.   
  12. class Widget : public QWidget  
  13. {  
  14.     Q_OBJECT  
  15.   
  16. public:  
  17.     explicit Widget(QWidget *parent = 0);  
  18.     ~Widget();  
  19.   
  20. private:  
  21.     One *one;  
  22.     Two *two;  
  23.     Three *three;  
  24.     QStackedLayout *stackLayout;  
  25.     QVBoxLayout *mainLayout;  
  26. };  
  27.   
  28. #endif // WIDGET_H  

widget.cpp

[cpp]  view plain  copy
  1. #include "widget.h"  
  2. #include "ui_widget.h"  
  3. #include "one.h"  
  4. #include "two.h"  
  5. #include "three.h"  
  6. #include <QStackedLayout>  
  7. #include <QPushButton>  
  8. #include <QVBoxLayout>  
  9.   
  10. Widget::Widget(QWidget *parent) :  
  11.     QWidget(parent)  
  12. {  
  13.     setFixedSize(400, 300);  
  14.     one = new One;  
  15.     two = new Two;  
  16.     three = new Three;  
  17.     stackLayout = new QStackedLayout;  
  18.     stackLayout->addWidget(one);  
  19.     stackLayout->addWidget(two);   
  20.     stackLayout->addWidget(three);  
  21.     connect(one, &One::display, stackLayout, &QStackedLayout::setCurrentIndex);             // 0  
  22.     connect(two, &Two::display, stackLayout, &QStackedLayout::setCurrentIndex);             // 1  
  23.     connect(three, &Three::display, stackLayout, &QStackedLayout::setCurrentIndex);       // 2  
  24.   
  25.     mainLayout = new QVBoxLayout;  
  26.     mainLayout->addLayout(stackLayout);  
  27.     setLayout(mainLayout);  
  28.   
  29. }  
  30.   
  31. Widget::~Widget()  
  32. {  
  33. }  

main.cpp

[cpp]  view plain  copy
  1. #include "widget.h"  
  2. #include <QApplication>  
  3.   
  4. int main(int argc, char *argv[])  
  5. {  
  6.     QApplication a(argc, argv);  
  7.     Widget w;  
  8.     w.show();  
  9.   
  10.     return a.exec();  
  11. 注:源代码下载地址http://download.csdn.net/detail/tj807126663/7540203
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值