最大化qt中单个实例应用程序的已运行实例
maximizing the already running instance of a single instance app in qt
我制作了一个应用程序,使用qt中的共享内存只运行一个实例
我的代码如下所示
int main(int argc, char *argv[])
{
QSharedMemory sharedMemory;
sharedMemory.setKey("4da7b8a28a378e7fa9004e7a95cf257f");
if(!sharedMemory.create(1))
{
return 1; // Exit already a process running
}
QApplication a(argc, argv);
Encoder *encoder = Encoder::instance();
encoder->show();
return a.exec();
}
现在,当用户尝试运行另一个实例时,我需要向用户显示已经运行的实例(最大化窗口)。我怎样才能做到这一点?
使用QtSingleApplication有一个简单的设置:
QtSingleApplication app("myapp",argc, argv);
if (app.isRunning()) {
QListIterator<QString> it(messagestosend);
QString rep("Another instance is running, so I will exit.");
bool sentok = false;
while(it.hasNext()){
sentok = app.sendMessage(it.next(),1000);
if(!sentok)
break;
}
rep += sentok ? " Message sent ok." : " Message sending failed; the other instance may be frozen.";
return 0;
}
要接收此消息,您应该用您想要的插槽收听信号
void QtSingleApplication::messageReceived(const QString&)
相关文章:
- 建议在运行时将带有类实例的列表从c++导入qml
- 我们如何并行运行算法的 n 个实例并以有效的方式计算结果函数的平均值?
- 当函数返回C++向量实例时,它正在运行
- 有没有办法根据命令行参数定义数组大小?运行时与编译时实例化?
- 使用to_string、reverse、stoi组合的C++反转编号给出运行时错误实例超出范围
- 非类型引用参数可以在运行时修改,这是否意味着模板可以在运行时实例化?
- 运行时检查实例 (Base*) 是否覆盖父函数 (Base::f())
- 根据运行时参数避免模板实例化的代码重复
- 获取派生模板实例化的运行时类型
- 如何根据运行时输入实例化 c++ 模板
- 确保只有一个函数实例在运行?
- 一次运行程序的多个实例C
- 在C 中运行时从指针访问对象实例
- JIT 编译的运行速度能否比编译时模板实例化更快?
- 析构函数在 lambda 捕获说明符中声明的类实例上运行两次
- 备份正在运行的 rocksdb 实例
- 使用运行时常量实例化的函数模板
- 在 QWidgets 线程的实例上运行 lambda 函数槽
- Python 可以运行脚本的多个实例,每个实例都包含自己的数据吗?
- C++类实例化是否可以在运行时更改其大小