点击(此处)折叠或打开
- //mainwindow.h
-
#ifndef MAINWINDOW_H
-
#define MAINWINDOW_H
-
-
#include <QMainWindow>
-
#include <QPushButton>
-
-
static const int EXIT_CODE_REBOOT = -123456789;
-
-
namespace Ui {
-
class MainWindow;
-
}
-
-
class MainWindow : public QMainWindow
-
{
-
Q_OBJECT
-
-
public:
-
explicit MainWindow(QWidget *parent = 0);
-
~MainWindow();
-
-
public slots:
-
void slotReboot();
-
-
private:
-
Ui::MainWindow *ui;
-
QPushButton * actionReboot;
-
};
-
- #endif // MAINWINDOW_H
点击(此处)折叠或打开
- //mainwindow.cpp
-
#include "mainwindow.h"
-
#include "ui_mainwindow.h"
-
#include <QDebug>
-
-
MainWindow::MainWindow(QWidget *parent) :
-
QMainWindow(parent),
-
ui(new Ui::MainWindow)
-
{
-
-
ui->setupUi(this);
-
-
setGeometry(100,100,300,200);
-
-
actionReboot =new QPushButton("restart!",this);//按键,设置其标识符restart,
-
actionReboot->setGeometry(10,20,80,40);//坐标位置、几何大小
-
actionReboot->setFont(QFont("Times",18,QFont::Bold));//设置字体
-
connect( actionReboot, SIGNAL(clicked()), this, SLOT(slotReboot()) );
-
}
-
-
MainWindow::~MainWindow()
-
{
-
delete ui;
-
}
-
-
void MainWindow::slotReboot()
-
{
-
qDebug() << "Performing application reboot..";
-
qApp->exit( EXIT_CODE_REBOOT );//调用exit(),退出应用程序。
-
qDebug() << "..........";
- }
点击(此处)折叠或打开
- //main.cpp
-
#include "mainwindow.h"
-
#include <QApplication>
-
-
int main(int argc, char *argv[])
-
{
-
int currentExitCode = 0;
-
QApplication a(argc, argv);
-
-
do{
-
MainWindow w;
-
w.show();
-
currentExitCode = a.exec();
-
-
}while( currentExitCode == EXIT_CODE_REBOOT );
-
-
return 0;
-
- }

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