Unsolved Newbie trying to work out how to run a file using QT Creator
-
I created a file called "run" using this command in Bash
"g++ fileA.cpp fileB.cpp run -o -std=c++11"If I open bash.exe (the command line), go to the directory where "run" is and type "./run" that runs it
What I'd like help with:
After clicking a button, I'd like QT to run the file called "run". The "run" file, however, can only be run through "bash.exe" and only if "./run" is typed into itAny ideas on how to do this?
-
Hi,
I'm not sure I understand, but check these:
-
@Publically-visible-name ./run is a local run. Do you need the full path for run in the bash script?
-
@Publically-visible-name said in Newbie trying to work out how to run a file using QT Creator:
The "run" file, however, can only be run through "bash.exe" and only if "./run" is typed into it
That is not so from what you have described.
bash.exe
need not be involved here --- unless you are passing certain kinds of arguments or certain shell operators on the command line you type into bash, which does not seem to be the case.If you want to run a subprocess from a Qt program you write then you can use
QProcess::start()
passing the full path to yourrun
executable. You can attach that action to user pressing a button in your application as per links @Abderrahmene_Rayene has provided.However, I am not sure you are really talking about writing a Qt application from which you want to execute external
run
? Maybe you are talking about pressing a button in Qt Creator to start therun
in a project you build with those source files?After clicking a button, I'd like QT to run the file called "run".
What does "QT" mean to you? Qt Creator is an IDE for developers to edit, compile, run and debug C++ programs they write. Qt is a library you can include and link to your C++ code to produce a program using e.g. a UI. Which of these two does your "QT" mean?
-
For more info about what I'm doing,
- I've made a C++ program
- I'd like to add a user interface to it using QT
I'd like to use QT creator to:
A) After clicking a button, open a few .csv files (worked that out)#include <QCoreApplication> #include <QDesktopServices> void MainWindow::on_openFile_clicked() { QString csvFileLocation = QCoreApplication::applicationDirPath() + "/csvfile.csv"; QDesktopServices::openUrl(csvFileLocation); }
B) Now, after clicking another button called "generateFile", I'd like to generate a file using this Bash command (no idea how to do this)
g++ [fileA].cpp [fileB].cpp generatedFile -o -std=c++11"
QString fileALocation = QCoreApplication::applicationDirPath() + "/fileA.cpp";
QString fileBLocation = QCoreApplication::applicationDirPath() + "/fileB.cpp";B) Then, after clicking another button called "runGeneratedFile", I'd like to run the generated file using this Bash command (no idea how to do this)
./generatedFile
QString generatedFileLocation = QCoreApplication::applicationDirPath() + "/generatedFile";
What I'd like to show when "./generatedFile" is entered?
I'd like this screen to show up. Because of what's inputted in the .csv file, it will show the outcome of a battle between a "Battering Ram" and "Town Center (Persian)". The C++ program is a combat results calculator for a board game
I hope that makes things a lot clearer, apologize for not being clearer at start
-
@JonB When I say "I'd like QT / QT creator to do X", I mean that I'm hoping that one of "QT"'s libraries like "QProcess" can help me do "X", which is what I'm trying to do but I have no idea which one if any of them to use since I'm completely new to using these libraries as well as the IDE
-
@Publically-visible-name
OK, I understand better what you would like to do, though I am not clear why you would like to do it...! :)Yes you can add UIs to C++ code by using Qt libraries. And typically you would do that by editing and compiling from Qt Creator. You'll have to read up on how to create a project in Creator, get your C++ source files into it, add Qt UI files, get it working.
You have discovered you can use Qt's
QDesktopServices::openUrl(csvFileLocation);
to ask the desktop to open a CSV file --- which presumably opens the file in some window as either a plain text file or something which knows about CSV files (e.g. an Excel-like window).Both the
g++
and the./generatedFile
are external commands. You presently know how to execute them from a terminal running bash. You can run external programs from a Qt application viaQProcess
. However there is nothing about these two commands which requires bash. They can both be run directly.Taking the second one first, a simple
QString generatedFileLocation = QCoreApplication::applicationDirPath() + "/generatedFile"; QProcess proc; proc.start(generatedFileLocation, {}); proc.waitForFinished();
will run
generatedFile
and wait for it to terminate.Similar for the first one. However rather than passing full paths to it (which you could do) my guess is it would work best if you made it *change directory" to where its files are:
QProcess proc; proc.setWorkingDirectory(QCoreApplication::applicationDirPath()); proc.start("g++", { "-std=c++11", "-o", "generatedFile", "fileA.cpp", "fileB.cpp" } ); proc.waitForFinished();
You can easily hook both of these up so they are performed when user clicks buttons.
I have not put error checking in. I have written code which waits for the sub-process to finish, blocking the parent UI application while they run. You may or may not want to address these.
However, we are left in (a) a strange situation and (b) we have a problem!
Firstly, it is very odd that you would want to run a compilation (
g++
) from your Qt UI application. This would requireg++
to be present at runtime of your UI application. Normally you, the coder, would do any compilations and supply the resulting executable code to your end user. That is just what Qt Creator is for. WHY are you "delaying" the compilation offileA.cpp
etc. to producegeneratedFile
till an end user runs your UI program? WHY aren't you doing this at the same time as producing your UI application?The C++ program is a combat results calculator for a board game
Unless you have a very strange requirement, you should be producing the compiled code of
generatedFile
in advance. If, say, its behaviour is to be altered by what is in some CSV files (or anything else) that data should be supplied at runtime somewhere/somehow andgeneratedFile
should read it in, parse it and act on it. Rather than requiring to be compiled.Secondly, we have a problem running your
generatedFile
the way you show from a UI application. DoesgeneratedFile
need a console window? If all it does is outputs lines of (colored) text you can do that OK from a Qt UI application in, say, aQTextEdit
window. However, if it inputs anything from the user as it goes, in between the output, then there is a problem: you won't have a console window where output and input alternate. If you need that you probably need to run it under Linux xterm (or similar) to obtain a console windowproc.start("xterm", { "-e", QCoreApplication::applicationDirPath() + "/generatedFile" });
The above should be enough to get you going :) I am intrigued to hear why you want to run a
g++
compilation at runtime...? -
For (B), the "generateFile" solution you provided creates the "generatedFile" and adds the .exe extension to it, which is fine. The extension of the file doesn't seem to matter (it can be .sh or whatever) and Bash can still run it but that's besides the point. That code works exactly how I was hoping it would. Excellent
Good to know that Bash isn't needed to do what we wanted for (B). I didn't know this until now))
To answer your questions about (B),
The only reason for why I haven't compiled this file on runtime is because the user can edit the .csv files, which will change how the file is compiled. Referring to the screenshot I added in this post, the user enters what's participating in the battle (e.g. a knight and samurai), which player has what, what technologies they have researched (e.g. If player 1 had "Iron Casting" that would give their cavalary +4 attack / SD), how many of each unit they have, etc. into the .csv file. As the game progresses, player 1 might, for example, research the technology, "Scale Barding Armor", which adds +2 HP to their cavalry and the .csv file will keep track that player 1 has both "Scale Barding Armor" and "Iron Casting" so their Knight in this example would receive +2 HP and +4 attack / SD
We can, however, not require the "end user" to click both "generateFile" button and "runGeneratedFile" button, and have just a "generateAndRunGeneratedFile" button. That way, the user only has to click one button))
For (A), the generatedFile does need a console window since some (but not much) user input is obtained. I put this code in and ran the program:
void MainWindow::on_runGeneratedFile_clicked() { QProcess proc; proc.start("xterm", { "-e", QCoreApplication::applicationDirPath() + "/generatedFile.exe"}); proc.waitForFinished(); }
There are no errors coming up too. The path it points to is correct and the file is there. But for some inexplicable reason no console window shows up. I wonder what the reason for that is. This is the "generatedFile.exe" file (if that helps with getting to the bottom of things)
https://1drv.ms/u/s!AkcO8igyaNXAlHOkag5sUcZU0Nmd?e=Igi5xwScreenshot of what it should look like when run (it has player 1's Cavalry & Monk now vs player 2's Samurai (Japanese) aha)
So I'd still appreciate some help with (A)
-
Cant add much more than @Abderrahmene_Rayene and @JonB have said before, but I would rethink the whole app design.
Why should the csv file, which includes some player and character stats, chance how your app is compiled?!
The whole thing looks like some "Pen and paper" kind of "game"?! -
So it's a "combat results calculator"
If the input changes (in this case, the input is things that are battling each other and modifiers to their values) THEN the calculations change and THEN the final result changes
Are you saying that when designing the C++ program, I could have made it so change can happen (input, calculations, and final result) without having to recompile the program?
I guess that this program is a reflection of my level of C++ knowledge at the time of doing this course (https://www.adelaide.edu.au/course-outlines/105877/1/sem-1/) a few years ago and I'm sure that someone with more expertise would have designed it better than I. When I was at uni at the time, I remember being bummed that they didn't even mention anything about adding a GUI to C++. I'm sure most end users wouldn't want to use a command line program. At least I'm learning something from you guys))
-
@Publically-visible-name said in Newbie trying to work out how to run a file using QT Creator:
The only reason for why I haven't compiled this file on runtime is because the user can edit the .csv files, which will change how the file is compiled.
It should not. Whatever data is in the CSV file should be read in and acted upon at runtime by a C++ program you write. You design your program to do whatever is necessary. Don't use it to produce new C++ source files for compilation.
"Variables" are called such because their values... vary. Use them! Lots of them :) They can take on different values at runtime, say read in from a CSV file. Rather than altering your source code with the values in CSV file and recompiling to produce a new executable program.
Are you saying that when designing the C++ program, I could have made it so change can happen (input, calculations, and final result) without having to recompile the program?
Of course. 100% :)
-
@JonB said in Newbie trying to work out how to run a file using QT Creator:
@Publically-visible-name said in Newbie trying to work out how to run a file using QT Creator:
The only reason for why I haven't compiled this file on runtime is because the user can edit the .csv files, which will change how the file is compiled.
It should not. Whatever data is in the CSV file should be read in and acted upon at runtime by a C++ program you write. You design your program to do whatever is necessary. Don't use it to produce new C++ source files for compilation.
"Variables" are called such because their values... vary. Use them! Lots of them :) They can take on different values at runtime, say read in from a CSV file. Rather than altering your source code with the values in CSV file and recompiling to produce a new executable program.
Are you saying that when designing the C++ program, I could have made it so change can happen (input, calculations, and final result) without having to recompile the program?
Of course. 100% :)
Okay, I just learned something (how did I not know this until now 🤦♂️ )
The "generatedFile" or "run" or whatever I've been calling it does not need to be recompiled every time)). I just thought it did.
If the input (the .csv file) changes, the calculations and output change, and I don't have to recompile everything. So the design / programming is all good, it's just me the user that doesn't know how to use my own program 🤣. I guess that makes more sense since then I wouldn't have to include the source files with the program. I'd just have to share the executable and the 'csv' files where user input goes. I honestly never thought about whether or not it was "smart" enough to do that
Maybe the reason I thought I had to recompile everything was because during development, I had to recompile it like a bazillion times (as I was changing the source code) so I needed to update the executable so it become like something I thought i had to do even when changing the .csv files, which contain user input))
-
In case it got lost in the wall of text, I'd still appreciate some help with working out how to make a console window show up where a user can see the result and enter input (0's or 1's)
I tried using this code
void MainWindow::on_runGeneratedFile_clicked() { QProcess proc; proc.start("xterm", { "-e", QCoreApplication::applicationDirPath() + "/generatedFile.exe"}); proc.waitForFinished(); }
This directory contains the file I'd like to run as well as the .csv files it imports (in the previous post, I forgot to include these .csv files, it won't run without them)
https://1drv.ms/f/s!AkcO8igyaNXAlHQIof37Z-i1_QSe?e=X5NSH0As soon as that's working, that's that problem solved
I'll still be around though, I'll see if I can learn more from reading through the forums, QT documentation, watching YouTube videos, etc. but I shouldn't need more help with this program after that. Thanks for all your help with everything too
-
@Publically-visible-name said in Newbie trying to work out how to run a file using QT Creator:
I tried using this code
You don't say what happens, whether it works or not.
Suggestion of
xterm
was because I thought you were under Linux, because you talk about bash. However, addition of.exe
to generated executable makes it apparently Windows. So which one are you? If Windows you may wantproc.start("cmd", { "/k", QCoreApplication::applicationDirPath() + "/generatedFile.exe" });
As I said earlier, it also can depend on whether that program only shows output or whether it requires the user to type anything into it. And maybe you don't need the Qt "UI" program with buttons at all --- a command-line Qt program run from the command line (i.e. a console) will have access to that console for read & write, the problem comes when spawning it from a UI program which does not have a console.
-
@JonB said in Newbie trying to work out how to run a file using QT Creator:
@Publically-visible-name said in Newbie trying to work out how to run a file using QT Creator:
I tried using this code
You don't say what happens, whether it works or not.
Suggestion of
xterm
was because I thought you were under Linux, because you talk about bash. However, addition of.exe
to generated executable makes it apparently Windows. So which one are you? If Windows you may wantproc.start("cmd", { "/k", QCoreApplication::applicationDirPath() + "/generatedFile.exe" });
As I said earlier, it also can depend on whether that program only shows output or whether it requires the user to type anything into it. And maybe you don't need the Qt "UI" program with buttons at all --- a command-line Qt program run from the command line (i.e. a console) will have access to that console for read & write, the problem comes when spawning it from a UI program which does not have a console.
I'm using the Windows OS but the file itself can only be run through Bash (from what I understand). I tested it and found that Bash could deal with the .exe file extension too
Reply to below comment:
I'm using Windows 10 with the "The Windows Subsystem for Linux"
Yeah, I have xterm installed
-
@Publically-visible-name
.exe
is a Windows-only extension for executable files. Linux has no (non-explicit) extension. bash is naturally available under Linux, might have been added under Windows. Looks like your case.You obviously have some Linux subsystem/commands. So do you have
xterm
? If not you will likely need to usecmd
instead. -
@Publically-visible-name said in Newbie trying to work out how to run a file using QT Creator:
Yeah, I have xterm installed
OK, so going back does the
xterm -e generatedFile.exe
I said and you showed work for you, or not? You can practice running it from, say, bash before trying to incorporate it into a Qt program.I also asked
- Do you really need the Qt UI pushbutton program, or is a non-UI console enough for you? Affects whether you will need xterm or not.
- Even if you need a UI, does the user need to type into
generatedFile
or does he only need to see its output?
-
@JonB said in Newbie trying to work out how to run a file using QT Creator:
@Publically-visible-name said in Newbie trying to work out how to run a file using QT Creator:
Yeah, I have xterm installed
OK, so going back does the
xterm -e generatedFile.exe
I said and you showed work for you, or not?I get this error running it through the command line (with or without the .exe extension)
To answer your questions, the user does type into the console (e.g. whether unit is retreating or not during combat)
And I would like to have a UI with buttons if poss.
Reply to below comment:
I entered the line "export DISPLAY=0.0"
Now, I get this error when I try to run the program with "xterm -e ./run"Apologize for not replying normally, I have a like 6 minute wait before I can reply so I'm editing this post aha
-
@Publically-visible-name
First answer my 2 bullet points in previous email.For the xterm to run in Linux subsystem you will need
DISPLAY
environment variable set, Try in that bash shell:export DISPLAY=0.0 xterm -e ....
-
@Publically-visible-name
[I have up-voted all your posts. That may free you from the delay before you can post again.]For the xterm error. You have a lot to read up on how WSL works then! I know nothing about it.
For now my suggestion would be to dump the idea of a Qt UI program and pressing a button to run
generatedFile
. Just get it working so that you can rungeneratedFile
from your bash shell in a console. Your Qt program should create aQCoreApplication
only, not aQApplication
. That will allow you to just read & write (via stdin/out) from/to the console.At some point soon you are going to have to do a lot of reading/finding examples. I don't intend to teach everything you will need!