How do I run an exe file on button clicked in a Qt app?
-
I have a set of console applications that I made in Visual Studio.
Now, instead of going folder by folder to open each of those console applications, I want to lay them out so they will look neat. To lay them out, of course, is to make a Qt program that has buttons on it that I, the user, will press in order to execute my desired console application.
The console applications are in .exe files and when I open them, they work. However, with Qt, I don't seem to make it work.
First, I tried system("../myapp.exe"), but it is not really a good choice. It opens up a new window, i.e. cmd, and my program works there, however, as long as that cmd window is open, it won't let me interact with Qt app.
I don't feel comfortable using system() so I searched the internet for other options.
So, I found QProcess and I tried to use it, like this:QProcess *process = new QProcess(this); QString file("\".../myapp.exe\""); process->execute(file);
The console application works but only inside the debug window of Qt and it won't let me do any kind of inputs.
What I want is a new window, which is a cmd window, to pop-up. And also I want to be able to interact too with the Qt app.
Please help, thanks in advance.
-
I have a set of console applications that I made in Visual Studio.
Now, instead of going folder by folder to open each of those console applications, I want to lay them out so they will look neat. To lay them out, of course, is to make a Qt program that has buttons on it that I, the user, will press in order to execute my desired console application.
The console applications are in .exe files and when I open them, they work. However, with Qt, I don't seem to make it work.
First, I tried system("../myapp.exe"), but it is not really a good choice. It opens up a new window, i.e. cmd, and my program works there, however, as long as that cmd window is open, it won't let me interact with Qt app.
I don't feel comfortable using system() so I searched the internet for other options.
So, I found QProcess and I tried to use it, like this:QProcess *process = new QProcess(this); QString file("\".../myapp.exe\""); process->execute(file);
The console application works but only inside the debug window of Qt and it won't let me do any kind of inputs.
What I want is a new window, which is a cmd window, to pop-up. And also I want to be able to interact too with the Qt app.
Please help, thanks in advance.
-
@S.ARE Hi,
You probably want to use QProcess::startDetached().
Arguments are optional of course.QProcess::startDetached("myexe", QStringList() << "any_argument");
@Gojir4 said in How do I run an exe file on button clicked in a Qt app?:
QStringList() << "any_argument"
I tried the code and it works, however, it didn't open any window.
What's the problem?EDIT:
I thought of a way of handling this problem.
I created a batch file with this code in it:cd %~dp0 start file.exe
And with that batch file, I accessed it in Qt by the code you told me:
QProcess::startDetached("\".../test.bat\"");
And it worked as I wanted it to be.
Though, if possible, I want to know how to open another cmd window using Qt instead of using
a batch file.Anyway, thanks.
-
@Gojir4 said in How do I run an exe file on button clicked in a Qt app?:
QStringList() << "any_argument"
I tried the code and it works, however, it didn't open any window.
What's the problem?EDIT:
I thought of a way of handling this problem.
I created a batch file with this code in it:cd %~dp0 start file.exe
And with that batch file, I accessed it in Qt by the code you told me:
QProcess::startDetached("\".../test.bat\"");
And it worked as I wanted it to be.
Though, if possible, I want to know how to open another cmd window using Qt instead of using
a batch file.Anyway, thanks.
@S.ARE said in How do I run an exe file on button clicked in a Qt app?:
QProcess::startDetached("".../test.bat"");
Note that you don't need to escape anything here, and there is no reason to use Windows path syntax. Qt understands (and automatically handles) unix-style paths on all platforms. So:
QProcess::startDetached("../test.bat");
is enough. Just be careful with such relative paths - that code won't work if you run it from some different directory (where test is not in the parent dir). If you want a more foolproof solution, chuck app dir in there:
QProcess::startDetached(QCoreApplication::instance()->applicationDirPath() + "/../test.bat");
Lastly, if you want to open some file using default app used for such files on given platform, you can use QDesktopServices::openUrl(). It's probably not necessary in this case, however.
-
@Gojir4 said in How do I run an exe file on button clicked in a Qt app?:
QStringList() << "any_argument"
I tried the code and it works, however, it didn't open any window.
What's the problem?EDIT:
I thought of a way of handling this problem.
I created a batch file with this code in it:cd %~dp0 start file.exe
And with that batch file, I accessed it in Qt by the code you told me:
QProcess::startDetached("\".../test.bat\"");
And it worked as I wanted it to be.
Though, if possible, I want to know how to open another cmd window using Qt instead of using
a batch file.Anyway, thanks.
@S.ARE
Still not 100% on what you want. You seem to be getting somewhere with @sierdzio'sstartDetached()
.cd %~dp0 start file.exe
Though, if possible, I want to know how to open another cmd window using Qt instead of using
a batch file.You should be able to achieve this directly, if that's what you want, via
QProcess::startDetached("cmd /k myapp.exe");
which will keep the opened terminal/console around after your app runs, not sure if that's what you mean.
If you need a
cd
to set the working directory, you can use the overload http://doc.qt.io/qt-5/qprocess.html#startDetached-1 :
bool QProcess::startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory = QString(), qint64 *pid = nullptr)
Does any of that get you closer to what you want?
-
@S.ARE said in How do I run an exe file on button clicked in a Qt app?:
QProcess::startDetached("".../test.bat"");
Note that you don't need to escape anything here, and there is no reason to use Windows path syntax. Qt understands (and automatically handles) unix-style paths on all platforms. So:
QProcess::startDetached("../test.bat");
is enough. Just be careful with such relative paths - that code won't work if you run it from some different directory (where test is not in the parent dir). If you want a more foolproof solution, chuck app dir in there:
QProcess::startDetached(QCoreApplication::instance()->applicationDirPath() + "/../test.bat");
Lastly, if you want to open some file using default app used for such files on given platform, you can use QDesktopServices::openUrl(). It's probably not necessary in this case, however.
-
@S.ARE
Still not 100% on what you want. You seem to be getting somewhere with @sierdzio'sstartDetached()
.cd %~dp0 start file.exe
Though, if possible, I want to know how to open another cmd window using Qt instead of using
a batch file.You should be able to achieve this directly, if that's what you want, via
QProcess::startDetached("cmd /k myapp.exe");
which will keep the opened terminal/console around after your app runs, not sure if that's what you mean.
If you need a
cd
to set the working directory, you can use the overload http://doc.qt.io/qt-5/qprocess.html#startDetached-1 :
bool QProcess::startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory = QString(), qint64 *pid = nullptr)
Does any of that get you closer to what you want?
@JonB
I tried your code, however, it didn't open a new cmd window. (The console application ran on the debug window, i.e. the "Application Output" tab.)About the ...
bool QProcess::startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory = QString(), qint64 *pid = nullptr)
I don't seem to understand how to use it.
Anyway, what I did is...
QString program("cmd /k cd " + QCoreApplication::applicationDirPath() + " & file.exe"); QProcess::startDetached(program);
-
@JonB
I tried your code, however, it didn't open a new cmd window. (The console application ran on the debug window, i.e. the "Application Output" tab.)About the ...
bool QProcess::startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory = QString(), qint64 *pid = nullptr)
I don't seem to understand how to use it.
Anyway, what I did is...
QString program("cmd /k cd " + QCoreApplication::applicationDirPath() + " & file.exe"); QProcess::startDetached(program);
@S.ARE Do not put parameters into same string as program:
QString program("cmd.exe"); QStringList parameters; parameters << " /k" << "cd " << QCoreApplication::applicationDirPath() << " & file.exe"); QProcess::startDetached(program, parameters); ``
-
@Gojir4 said in How do I run an exe file on button clicked in a Qt app?:
QStringList() << "any_argument"
I tried the code and it works, however, it didn't open any window.
What's the problem?EDIT:
I thought of a way of handling this problem.
I created a batch file with this code in it:cd %~dp0 start file.exe
And with that batch file, I accessed it in Qt by the code you told me:
QProcess::startDetached("\".../test.bat\"");
And it worked as I wanted it to be.
Though, if possible, I want to know how to open another cmd window using Qt instead of using
a batch file.Anyway, thanks.
@S-ARE said in How do I run an exe file on button clicked in a Qt app?:
cd %~dp0
start file.exeAWESOME SOLUTION. THE NEW PROCESS WON'T BLOCK MAIN PROCESS, AND ALSO OUTPUT TO THE CMD WINDOW.
ONE STEP , MULTI TARGET。MANY BIRDS WITH ONE STONE~