Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved Newbie trying to work out how to run a file using QT Creator

    General and Desktop
    5
    27
    154
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      Publically visible name last edited by

      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 it

      Any ideas on how to do this?

      JoeCFD JonB 2 Replies Last reply Reply Quote 0
      • Abderrahmene_Rayene
        Abderrahmene_Rayene last edited by

        Hi,

        I'm not sure I understand, but check these:

        • Qt pushButton trigger terminal command

        • How to Use QPushButton

        • QProcess, QProcess::startCommand

        I started making desktop apps: https://pling.com/u/rayenemhb
        Enjoy my obscure questions: https://stackoverflow.com/users/17726418/mihoub-abderrahmene-rayene

        1 Reply Last reply Reply Quote 0
        • JoeCFD
          JoeCFD @Publically visible name last edited by

          @Publically-visible-name ./run is a local run. Do you need the full path for run in the bash script?

          1 Reply Last reply Reply Quote 0
          • JonB
            JonB @Publically visible name last edited by JonB

            @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 your run 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 the run 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?

            P 2 Replies Last reply Reply Quote 1
            • P
              Publically visible name @JonB last edited by Publically visible name

              @JonB

              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

              c883d24a-9a63-490b-8718-d71322c25b19-image.png

              I hope that makes things a lot clearer, apologize for not being clearer at start

              JonB 1 Reply Last reply Reply Quote 1
              • P
                Publically visible name @JonB last edited by Publically visible name

                @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

                1 Reply Last reply Reply Quote 1
                • JonB
                  JonB @Publically visible name last edited by JonB

                  @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 via QProcess. 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 require g++ 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 of fileA.cpp etc. to produce generatedFile 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 and generatedFile 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. Does generatedFile 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, a QTextEdit 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 window

                  proc.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...?

                  P 1 Reply Last reply Reply Quote 3
                  • P
                    Publically visible name @JonB last edited by Publically visible name

                    @JonB

                    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=Igi5xw

                    Screenshot of what it should look like when run (it has player 1's Cavalry & Monk now vs player 2's Samurai (Japanese) aha)
                    ce4b2fdd-c600-4c9b-ae2c-6a93a57796ba-image.png

                    So I'd still appreciate some help with (A)

                    Pl45m4 JonB 2 Replies Last reply Reply Quote 1
                    • Pl45m4
                      Pl45m4 @Publically visible name last edited by Pl45m4

                      @Publically-visible-name

                      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"?!


                      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                      ~E. W. Dijkstra

                      P 1 Reply Last reply Reply Quote 1
                      • P
                        Publically visible name @Pl45m4 last edited by Publically visible name

                        @Pl45m4

                        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))

                        1 Reply Last reply Reply Quote 1
                        • JonB
                          JonB @Publically visible name last edited by JonB

                          @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% :)

                          P 1 Reply Last reply Reply Quote 3
                          • P
                            Publically visible name @JonB last edited by Publically visible name

                            @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))

                            1 Reply Last reply Reply Quote 1
                            • P
                              Publically visible name last edited by Publically visible name

                              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=X5NSH0

                              As 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

                              JonB 1 Reply Last reply Reply Quote 1
                              • JonB
                                JonB @Publically visible name last edited by JonB

                                @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 want

                                proc.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.

                                P 1 Reply Last reply Reply Quote 0
                                • P
                                  Publically visible name @JonB last edited by Publically visible name

                                  @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 want

                                  proc.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
                                  e0d803bd-8e7f-42a5-ab89-03376867e2fa-image.png

                                  JonB 2 Replies Last reply Reply Quote 1
                                  • JonB
                                    JonB @Publically visible name last edited by JonB

                                    @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 use cmd instead.

                                    1 Reply Last reply Reply Quote 0
                                    • JonB
                                      JonB @Publically visible name last edited by JonB

                                      @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?
                                      P 1 Reply Last reply Reply Quote 0
                                      • P
                                        Publically visible name @JonB last edited by Publically visible name

                                        @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)

                                        05ffc0e0-2632-455e-949f-4b4815bf92a9-image.png

                                        To answer your questions, the user does type into the console (e.g. whether unit is retreating or not during combat)
                                        2204c9e3-1b4b-438b-b1f5-d3e7aa52e133-image.png

                                        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"

                                        3c719662-f053-4dfc-b058-c6bca59add06-image.png

                                        Apologize for not replying normally, I have a like 6 minute wait before I can reply so I'm editing this post aha

                                        JonB 2 Replies Last reply Reply Quote 1
                                        • JonB
                                          JonB @Publically visible name last edited by

                                          @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 ....
                                          
                                          1 Reply Last reply Reply Quote 0
                                          • JonB
                                            JonB @Publically visible name last edited by

                                            @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 run generatedFile from your bash shell in a console. Your Qt program should create a QCoreApplication only, not a QApplication. 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!

                                            P 1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post