Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do I run an exe file on button clicked in a Qt app?
Forum Update on Monday, May 27th 2025

How do I run an exe file on button clicked in a Qt app?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 6 Posters 12.2k Views
  • 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.
  • S.ARES Offline
    S.ARES Offline
    S.ARE
    wrote on last edited by
    #1

    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.

    Gojir4G 1 Reply Last reply
    0
    • S.ARES S.ARE

      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.

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by
      #2

      @S.ARE Hi,
      You probably want to use QProcess::startDetached().
      Arguments are optional of course.

      QProcess::startDetached("myexe", QStringList() << "any_argument");
      
      S.ARES 1 Reply Last reply
      3
      • S.ARES Offline
        S.ARES Offline
        S.ARE
        wrote on last edited by S.ARE
        #3

        @Gojir4 Thank you but,

        Care to explain how to use it? As you can see, I'm still a beginner. :)

        Oops, sorry, I didn't see the example.

        1 Reply Last reply
        1
        • Gojir4G Gojir4

          @S.ARE Hi,
          You probably want to use QProcess::startDetached().
          Arguments are optional of course.

          QProcess::startDetached("myexe", QStringList() << "any_argument");
          
          S.ARES Offline
          S.ARES Offline
          S.ARE
          wrote on last edited by S.ARE
          #4

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

          sierdzioS JonBJ X 3 Replies Last reply
          1
          • S.ARES S.ARE

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

            sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

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

            (Z(:^

            S.ARES 1 Reply Last reply
            3
            • S.ARES S.ARE

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

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @S.ARE
              Still not 100% on what you want. You seem to be getting somewhere with @sierdzio's startDetached().

              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.ARES 1 Reply Last reply
              1
              • sierdzioS sierdzio

                @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.ARES Offline
                S.ARES Offline
                S.ARE
                wrote on last edited by
                #7

                @sierdzio I don't know why but the file won't start without the escape sequences.
                And, I already implemented that code:

                QProcess::startDetached(QCoreApplication::instance()->applicationDirPath() + "/../test.bat");
                

                Anyway, thanks for helping out.

                1 Reply Last reply
                0
                • JonBJ JonB

                  @S.ARE
                  Still not 100% on what you want. You seem to be getting somewhere with @sierdzio's startDetached().

                  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.ARES Offline
                  S.ARES Offline
                  S.ARE
                  wrote on last edited by
                  #8

                  @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);
                  
                  jsulmJ 1 Reply Last reply
                  1
                  • S.ARES S.ARE

                    @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);
                    
                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

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

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    3
                    • S.ARES S.ARE

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

                      X Offline
                      X Offline
                      xkungfu
                      wrote on last edited by xkungfu
                      #10

                      @S-ARE said in How do I run an exe file on button clicked in a Qt app?:

                      cd %~dp0
                      start file.exe

                      AWESOME 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~

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved