我想知道如何将命令行参数传递给已经打开的程序。因此,用户运行“backup.exe -job awef”,backup.exe 识别出已经打开的进程并将参数传递给已打开的进程,以便用户可以命令程序从命令行或快捷方式执行他们想要的操作在 window 里。
谢谢!
最佳答案
您可以根据 https://doc.qt.io/archives/qtextended4.4/qtopiadesktop/qtsingleapplication.html#QtSingleApplication 使用 QtSingleApplication 处理 messageReceived 上的命令行参数来实现您的目标。
在应用程序开始时,您需要检查是否可以将带有命令行参数的 sendMessage 发送到已运行的实例,然后退出。否则,您将继续启动您的应用程序,如下所示
int main(int argc, char* argv[])
{
QtSingleApplication app("MySingleInstance", argc, argv);
// try to send commandline arguments
if(app.sendMessage(app.arguments().join("$")))
{
return 0;
}
/* connect your messageRecieved signal to slot
SomeClass::slotLoadCommandLine to be able to handle
the commandline arguments from sendMessage*/
QObject::connect(qApp, SIGNAL(messageReceived(QString)),
SomeClass, SLOT(slotLoadCommandLine(QString)));
//start your application
return app.exec();
}
编辑
改为 SingleApplication
https://forum.qt.io/topic/22460/solved-qtsingleapplication-and-qt-5/5
How to use QtSingleApplication?
关于qt - 如何将命令行参数传递给 QT 中已经打开的程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39421166/