Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1390604
  • 博文数量: 120
  • 博客积分: 182
  • 博客等级: 入伍新兵
  • 技术积分: 2278
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-19 16:31
文章分类

全部博文(120)

文章存档

2015年(12)

2014年(13)

2013年(40)

2012年(55)

分类: C/C++

2013-10-25 18:48:30

原文出自:

点击(此处)折叠或打开

  1. //mainwindow.h
  2. #ifndef MAINWINDOW_H
  3. #define MAINWINDOW_H

  4. #include <QMainWindow>
  5. #include <QPushButton>

  6. static const int EXIT_CODE_REBOOT = -123456789;

  7. namespace Ui {
  8. class MainWindow;
  9. }

  10. class MainWindow : public QMainWindow
  11. {
  12.     Q_OBJECT
  13.     
  14. public:
  15.     explicit MainWindow(QWidget *parent = 0);
  16.     ~MainWindow();

  17. public slots:
  18.     void slotReboot();

  19. private:
  20.     Ui::MainWindow *ui;
  21.     QPushButton * actionReboot;
  22. };

  23. #endif // MAINWINDOW_H

点击(此处)折叠或打开

  1. //mainwindow.cpp
  2. #include "mainwindow.h"
  3. #include "ui_mainwindow.h"
  4. #include <QDebug>

  5. MainWindow::MainWindow(QWidget *parent) :
  6.     QMainWindow(parent),
  7.     ui(new Ui::MainWindow)
  8. {

  9.     ui->setupUi(this);

  10.     setGeometry(100,100,300,200);

  11.     actionReboot =new QPushButton("restart!",this);//按键,设置其标识符restart,
  12.     actionReboot->setGeometry(10,20,80,40);//坐标位置、几何大小
  13.     actionReboot->setFont(QFont("Times",18,QFont::Bold));//设置字体
  14.     connect( actionReboot, SIGNAL(clicked()), this, SLOT(slotReboot()) );
  15. }

  16. MainWindow::~MainWindow()
  17. {
  18.     delete ui;
  19. }

  20. void MainWindow::slotReboot()
  21. {
  22.     qDebug() << "Performing application reboot..";
  23.     qApp->exit( EXIT_CODE_REBOOT );//调用exit(),退出应用程序。
  24.     qDebug() << "..........";
  25. }

点击(此处)折叠或打开

  1. //main.cpp
  2. #include "mainwindow.h"
  3. #include <QApplication>

  4. int main(int argc, char *argv[])
  5. {
  6.     int currentExitCode = 0;
  7.     QApplication a(argc, argv);

  8.     do{
  9.         MainWindow w;
  10.         w.show();
  11.         currentExitCode = a.exec();

  12.     }while( currentExitCode == EXIT_CODE_REBOOT );

  13.     return 0;

  14. }
结果:
       当鼠标点击“restart!”按钮之后,就会触发一个clicked信号给连接的槽函数。然后执行槽函数。当Qt完成事件处理,应用程序推出的时候,a.exec()的值就会返回(在exec()中Qt接收并处理用户和系统的事件,并把它们传递给适当的窗口部件)。如果exec()的返回值与既定的常量值相等,则会重新启动该应用程序。


阅读(7906) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~