当前位置:网站首页>Qt实战案例(56)——利用QProcess实现应用程序重启功能
Qt实战案例(56)——利用QProcess实现应用程序重启功能
2022-08-01 12:49:00 【wendy_ya】
一、项目介绍
本文介绍利用QProcess实现应用程序重启功能。实现应用程序重启功能在Qt中实现非常简单,需要用到QProcess类一个静态方法:bool startDetached(const QString &program, const QStringList &arguments);
二、项目基本配置
新建一个Qt案例,项目名称为“RestartTest”,基类选择“QWidget”,点击选中创建UI界面复选框,完成项目创建。
三、UI界面设置
UI界面如下:
| 序号 | 名称 | 类型 | 属性 |
|---|---|---|---|
| ① | pbn_restart | QPushButton | text:Restart |
四、主程序实现
4.1 widget.h头文件
声明头文件:
private slots:
void on_pbn_restart_clicked();
定义RETCODE_RESTART:
// define a retcode: 773 = 'r'+'e'+'s'+'t'+'a'+'r'+'t' = restart
static const int RETCODE_RESTART = 773;
4.2 widget.cpp源文件
定义按钮点击槽函数:
//应用程序重启
void Widget::on_pbn_restart_clicked()
{
qApp->exit(RETCODE_RESTART);
}
4.3 main.cpp源文件
在main函数中判断退出码是否为“RETCODE_RESTART”,来决定是否重启:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
//return a.exec();
int e = a.exec();
if(e == RETCODE_RESTART)
{
// 传入 qApp->applicationFilePath(),启动自己
QProcess::startDetached(qApp->applicationFilePath(), QStringList());
return 0;
}
return e;
}
五、效果演示
完整效果如下:
如果没有看懂的话,完整代码可以参考:https://download.csdn.net/download/didi_ya/86268703
ok,以上便是本文的全部内容了,如果对你有所帮助,记得点个赞哟~
边栏推荐
猜你喜欢

CloudCompare & PCL ICP registration (point to face)

8. How does the SAP ABAP OData service support the Create operation

阿里云官方 Redis 开发规范

What is consistent hashing?In what scenarios can it be applied?

观察者模式

芝加哥丰田技术学院 | Leveraging Natural Supervision for Language Representation Learning and Generation(利用自然监督进行语言表示学习和生成)

Find objects with the same property value Cumulative number Summarize

批量替换Word中的表格为图片并保存

脚本语言Lua的基础知识总结

Multi-threaded cases - blocking queue
随机推荐
【StoneDB Class】入门第二课:StoneDB 整体架构解析
PanGu-Coder:函数级的代码生成模型
How to get the address of WeChat video account (link address of WeChat public account)
AI目标分割能力,无需绿幕即可实现快速视频抠图
This article will take you to thoroughly clarify the working mechanism of certificates in Isito
如何设计一个分布式 ID 发号器?
Grafana9.0发布,Prometheus和Loki查询生成器、全新导航、热图面板等新功能!
[Cloud Enjoying Freshness] Community Weekly Vol.73- DTSE Tech Talk: 1 hour in-depth interpretation of SaaS application system design
华盛顿大学、Allen AI 等联合 | RealTime QA: What's the Answer Right Now?(实时 QA:现在的答案是什么?)
什么是一致性哈希?可以应用在哪些场景?
tensorflow2.0 handwritten digit recognition (tensorflow handwriting recognition)
关于亚马逊测评,你了解多少?
一文带你彻底厘清 Kubernetes 中的证书工作机制
数据挖掘-04
那些利用假期学习的职场人,后来都怎么样了?
程序员的自我修养
SQL函数 %SQLUPPER
Istio投入生产的障碍以及如何解决这些问题
Find objects with the same property value Cumulative number Summarize
阿里云官方 Redis 开发规范