做一个无法关闭的QT程序(想关闭时要在任务管理器里关闭),看似很难,
其实它并不难,只要让程序在关闭时启动它自身就可以了。
上代码:
[cpp] view plaincopyprint?
- #include <QtGui>
- class Temp : public QWidget
- {
- Q_OBJECT
- private:
- QLabel *label;
- protected:
- void closeEvent(QCloseEvent *event);
- public:
- Temp(QWidget *parent = 0);
- ~Temp();
- };
- Temp::Temp(QWidget *parent)
- : QWidget(parent)
- {
- label = new QLabel("You can‘t close me, haha.", this);
- QVBoxLayout *layout = new QVBoxLayout;
- layout->addWidget(label);
- setLayout(layout);
- move(200, 200);
- }
- Temp::~Temp()
- {
- }
- void Temp::closeEvent(QCloseEvent *event)
- {
- //重载关系事件函数,使程序在关闭自己的同时重新打开自己
- QProcess *p = new QProcess(this);
- QString str = QApplication::applicationFilePath();
- p->startDetached(str);
- }
- #include "main.moc"
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
- Temp *temp = new Temp;
- temp->show();
- return app.exec();
- }
http://blog.csdn.net/small_qch/article/details/6704036
时间: 2024-10-10 08:35:34