【问题标题】:maximizing the already running instance of a single instance app in qt在qt中最大化单个实例应用程序的已经运行的实例
【发布时间】:2014-10-07 10:52:45
【问题描述】:

我在 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();
}

现在,当用户尝试运行另一个实例时,我需要向用户显示已经运行的实例(最大化窗口)。我怎样才能做到这一点?

【问题讨论】:

  • 你可以使用共享内存在运行实例中设置一个标志告诉它最大化吗?

标签: c++ qt shared-memory single-instance


【解决方案1】:

使用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&)

【讨论】:

  • 我已经构建了 qtsingleapplication dll。你能告诉我如何在我的项目中使用它吗?
  • using 是什么意思?链接dll?
  • 是的,我必须先链接 dll 才能使用它吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-23
  • 1970-01-01
相关资源
最近更新 更多