如何从 Qt 程序 运行 一个外部应用程序?
How to run an external application from Qt program?
我正在尝试从我的 Qt 应用程序 运行 一个外部 exe。这是一个 "autorun application",它有三个按钮:一个是 运行 一个外部安装程序,它是一个 .exe 应用程序。
我试过了:
system("Setup.exe")
它可以工作,但是在 运行安装程序时它会显示终端。我也试过:
QProcess::startDetached("Setup.exe");
也尝试过:
QProcess *process = new QProcess(this);
process->start("Setup.exe");
但都不起作用(没有任何反应,控制台输出中也没有日志)。有人可以指出我做错了什么或提出更好的解决方案吗?
谢谢。
这里我尝试检查所有可能的问题。此代码将启动您的进程或显示它不想工作的原因。
#include <QtCore/QCoreApplication>
#include <QProcess>
#include <QFileInfo>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString pathToExecutable = "c:\Apps\MyApp\Setup.exe";
// Check the path string was written correctly and Qt can see it
QFileInfo fileInfo(pathToExecutable);
QProcess process;
if (fileInfo.exists())
{
if(fileInfo.isExecutable())
{
QObject::connect(&process, &QProcess::errorOccurred,
&process, [&]()
{
qWarning() << process.errorString();
});
/*
Qt doc:
To set a working directory, call setWorkingDirectory().
By default, processes are run in the current
working directory of the calling process.
*/
QString executableDir = fileInfo.absolutePath();
process.setWorkingDirectory(executableDir);
QString absuluteExecutableFilePath = fileInfo.absoluteFilePath();
/*
Even if QFileInfo see your file, QProcess may not run it,
if the path contains spaces
*/
if (absuluteExecutableFilePath.contains(' '));
{
absuluteExecutableFilePath.prepend("\"").append("\"");
}
// If you need to check errors, use start(), but not startDetached()
process.start(absuluteExecutableFilePath);
}
else
{
qWarning() << "Given file is not executable";
}
}
else
{
qWarning() << "File path does not exist or is set incorrectly";
}
return a.exec();
}
我正在尝试从我的 Qt 应用程序 运行 一个外部 exe。这是一个 "autorun application",它有三个按钮:一个是 运行 一个外部安装程序,它是一个 .exe 应用程序。
我试过了:
system("Setup.exe")
它可以工作,但是在 运行安装程序时它会显示终端。我也试过:
QProcess::startDetached("Setup.exe");
也尝试过:
QProcess *process = new QProcess(this);
process->start("Setup.exe");
但都不起作用(没有任何反应,控制台输出中也没有日志)。有人可以指出我做错了什么或提出更好的解决方案吗?
谢谢。
这里我尝试检查所有可能的问题。此代码将启动您的进程或显示它不想工作的原因。
#include <QtCore/QCoreApplication>
#include <QProcess>
#include <QFileInfo>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString pathToExecutable = "c:\Apps\MyApp\Setup.exe";
// Check the path string was written correctly and Qt can see it
QFileInfo fileInfo(pathToExecutable);
QProcess process;
if (fileInfo.exists())
{
if(fileInfo.isExecutable())
{
QObject::connect(&process, &QProcess::errorOccurred,
&process, [&]()
{
qWarning() << process.errorString();
});
/*
Qt doc:
To set a working directory, call setWorkingDirectory().
By default, processes are run in the current
working directory of the calling process.
*/
QString executableDir = fileInfo.absolutePath();
process.setWorkingDirectory(executableDir);
QString absuluteExecutableFilePath = fileInfo.absoluteFilePath();
/*
Even if QFileInfo see your file, QProcess may not run it,
if the path contains spaces
*/
if (absuluteExecutableFilePath.contains(' '));
{
absuluteExecutableFilePath.prepend("\"").append("\"");
}
// If you need to check errors, use start(), but not startDetached()
process.start(absuluteExecutableFilePath);
}
else
{
qWarning() << "Given file is not executable";
}
}
else
{
qWarning() << "File path does not exist or is set incorrectly";
}
return a.exec();
}