无法关闭的QT程序(覆盖closeEvent,新建QProcess并脱离关系)

做一个无法关闭的QT程序(想关闭时要在任务管理器里关闭),看似很难,

其实它并不难,只要让程序在关闭时启动它自身就可以了。

上代码:

  1. #include <QtGui>    
  2.   
  3. class Temp : public QWidget    
  4. {      
  5.     Q_OBJECT      
  6. private:      
  7.     QLabel *label;    
  8. protected:    
  9.     void closeEvent(QCloseEvent *event);    
  10. public:      
  11.     Temp(QWidget *parent = 0);      
  12.     ~Temp();      
  13. };    
  14.   
  15. Temp::Temp(QWidget *parent)      
  16. : QWidget(parent)      
  17. {      
  18.     label = new QLabel("You can't close me, haha.", this);    
  19.     QVBoxLayout *layout = new QVBoxLayout;    
  20.     layout->addWidget(label);    
  21.     setLayout(layout);    
  22.     move(200, 200);    
  23. }      
  24.   
  25. Temp::~Temp()      
  26. {      
  27.   
  28. }    
  29.   
  30. void Temp::closeEvent(QCloseEvent *event)    
  31. {    
  32.     //重载关系事件函数,使程序在关闭自己的同时重新打开自己  
  33.     QProcess *p = new QProcess(this);    
  34.     QString str = QApplication::applicationFilePath();    
  35.     p->startDetached(str);    
  36. }    
  37.   
  38. #include "main.moc"  
  39.   
  40. int main(int argc, char *argv[])    
  41. {    
  42.     QApplication app(argc, argv);    
  43.     Temp *temp = new Temp;    
  44.     temp->show();    
  45.     return app.exec();    
  46. }      

http://blog.csdn.net/small_qch/article/details/6704036

原文地址:https://www.cnblogs.com/findumars/p/4851277.html