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. QProcess sample
Forum Updated to NodeBB v4.3 + New Features

QProcess sample

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 6 Posters 1.8k Views 2 Watching
  • 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.
  • J Offline
    J Offline
    JohnLocke
    wrote on last edited by
    #1

    Trying to compile test sample from official Qt documentation:

    #include <QCoreApplication>
    #include <QProcess>
    
    int main(int argc, char *argv[])
    {
    
    
    
        QCoreApplication a(argc, argv);
        QProcess process;
        
    //    process.setCreateProcessArgumentsModifier([] (QProcess::CreateProcessArguments *args)
    //                                              {
    //                                                  args->flags |= CREATE_NEW_CONSOLE;
    //                                                  args->startupInfo->dwFlags &= ~STARTF_USESTDHANDLES;
    //                                                  args->startupInfo->dwFlags |= STARTF_USEFILLATTRIBUTE;
    //                                                  args->startupInfo->dwFillAttribute = BACKGROUND_BLUE | FOREGROUND_RED
    //                                                                                       | FOREGROUND_INTENSITY;
    //                                              });
    //    process.start("C:\\Windows\\System32\\cmd.exe", QStringList() << "/k" << "title" << "The Child Process");
    
    
        return a.exec();
    }
    

    Gettings errors below.
    Using Ubuntu Jammy Fish, Qt 6.2.4, gcc

    04:03:25: Running steps for project untitled...
    04:03:25: Configuration unchanged, skipping qmake step.
    04:03:25: Starting: "/usr/bin/make" -j8
    g++ -c -pipe -g -std=gnu++1z -Wall -Wextra -D_REENTRANT -fPIC -DQT_QML_DEBUG -DQT_CORE_LIB -I../untitled -I. -I../Qt/6.2.4/gcc_64/include -I../Qt/6.2.4/gcc_64/include/QtCore -I. -I../Qt/6.2.4/gcc_64/mkspecs/linux-g++ -o main.o ../untitled/main.cpp
    ../untitled/main.cpp: In function ‘int main(int, char**)’:
    ../untitled/main.cpp:11:13: error: ‘class QProcess’ has no member named ‘setCreateProcessArgumentsModifier’
    11 | process.setCreateProcessArgumentsModifier([] (QProcess::CreateProcessArguments args)
    | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ../untitled/main.cpp:11:51: error: ‘QProcess::CreateProcessArguments’ has not been declared
    11 | process.setCreateProcessArgumentsModifier([] (QProcess::CreateProcessArguments args)
    | ^~~~~~~~
    ../untitled/main.cpp: In lambda function:
    ../untitled/main.cpp:13:57: error: request for member ‘flags’ in ‘
    args’, which is of non-class type ‘int’
    13 | args->flags |= CREATE_NEW_CONSOLE;
    | ^~~~~
    ../untitled/main.cpp:13:66: error: ‘CREATE_NEW_CONSOLE’ was not declared in this scope
    13 | args->flags |= CREATE_NEW_CONSOLE;
    | ^~~~~~~~~~~~~~~~~~
    ../untitled/main.cpp:14:57: error: request for member ‘startupInfo’ in ‘
    args’, which is of non-class type ‘int’
    14 | args->startupInfo->dwFlags &= ~STARTF_USESTDHANDLES;
    | ^~~~~~~~~~~
    ../untitled/main.cpp:14:82: error: ‘STARTF_USESTDHANDLES’ was not declared in this scope
    14 | args->startupInfo->dwFlags &= ~STARTF_USESTDHANDLES;
    | ^~~~~~~~~~~~~~~~~~~~
    ../untitled/main.cpp:15:57: error: request for member ‘startupInfo’ in ‘* args’, which is of non-class type ‘int’
    15 | args->startupInfo->dwFlags |= STARTF_USEFILLATTRIBUTE;
    | ^~~~~~~~~~~
    ../untitled/main.cpp:15:81: error: ‘STARTF_USEFILLATTRIBUTE’ was not declared in this scope
    15 | args->startupInfo->dwFlags |= STARTF_USEFILLATTRIBUTE;
    | ^~~~~~~~~~~~~~~~~~~~~~~
    ../untitled/main.cpp:16:57: error: request for member ‘startupInfo’ in ‘* args’, which is of non-class type ‘int’
    16 | args->startupInfo->dwFillAttribute = BACKGROUND_BLUE | FOREGROUND_RED
    | ^~~~~~~~~~~
    ../untitled/main.cpp:16:88: error: ‘BACKGROUND_BLUE’ was not declared in this scope
    16 | args->startupInfo->dwFillAttribute = BACKGROUND_BLUE | FOREGROUND_RED
    | ^~~~~~~~~~~~~~~
    ../untitled/main.cpp:16:106: error: ‘FOREGROUND_RED’ was not declared in this scope
    16 | args->startupInfo->dwFillAttribute = BACKGROUND_BLUE | FOREGROUND_RED
    | ^~~~~~~~~~~~~~
    ../untitled/main.cpp:17:90: error: ‘FOREGROUND_INTENSITY’ was not declared in this scope
    17 | | FOREGROUND_INTENSITY;
    | ^~~~~~~~~~~~~~~~~~~~
    make: *** [Makefile:981: main.o] Error 1
    04:03:26: The process "/usr/bin/make" exited with code 2.
    Error while building/deploying project untitled (kit: Desktop Qt 6.2.4 GCC 64bit)
    When executing step "Make"
    04:03:26: Elapsed time: 00:01.

    Pl45m4P 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Aren't you trying to build on Linux a Windows specific example ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • J JohnLocke

        Trying to compile test sample from official Qt documentation:

        #include <QCoreApplication>
        #include <QProcess>
        
        int main(int argc, char *argv[])
        {
        
        
        
            QCoreApplication a(argc, argv);
            QProcess process;
            
        //    process.setCreateProcessArgumentsModifier([] (QProcess::CreateProcessArguments *args)
        //                                              {
        //                                                  args->flags |= CREATE_NEW_CONSOLE;
        //                                                  args->startupInfo->dwFlags &= ~STARTF_USESTDHANDLES;
        //                                                  args->startupInfo->dwFlags |= STARTF_USEFILLATTRIBUTE;
        //                                                  args->startupInfo->dwFillAttribute = BACKGROUND_BLUE | FOREGROUND_RED
        //                                                                                       | FOREGROUND_INTENSITY;
        //                                              });
        //    process.start("C:\\Windows\\System32\\cmd.exe", QStringList() << "/k" << "title" << "The Child Process");
        
        
            return a.exec();
        }
        

        Gettings errors below.
        Using Ubuntu Jammy Fish, Qt 6.2.4, gcc

        04:03:25: Running steps for project untitled...
        04:03:25: Configuration unchanged, skipping qmake step.
        04:03:25: Starting: "/usr/bin/make" -j8
        g++ -c -pipe -g -std=gnu++1z -Wall -Wextra -D_REENTRANT -fPIC -DQT_QML_DEBUG -DQT_CORE_LIB -I../untitled -I. -I../Qt/6.2.4/gcc_64/include -I../Qt/6.2.4/gcc_64/include/QtCore -I. -I../Qt/6.2.4/gcc_64/mkspecs/linux-g++ -o main.o ../untitled/main.cpp
        ../untitled/main.cpp: In function ‘int main(int, char**)’:
        ../untitled/main.cpp:11:13: error: ‘class QProcess’ has no member named ‘setCreateProcessArgumentsModifier’
        11 | process.setCreateProcessArgumentsModifier([] (QProcess::CreateProcessArguments args)
        | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        ../untitled/main.cpp:11:51: error: ‘QProcess::CreateProcessArguments’ has not been declared
        11 | process.setCreateProcessArgumentsModifier([] (QProcess::CreateProcessArguments args)
        | ^~~~~~~~
        ../untitled/main.cpp: In lambda function:
        ../untitled/main.cpp:13:57: error: request for member ‘flags’ in ‘
        args’, which is of non-class type ‘int’
        13 | args->flags |= CREATE_NEW_CONSOLE;
        | ^~~~~
        ../untitled/main.cpp:13:66: error: ‘CREATE_NEW_CONSOLE’ was not declared in this scope
        13 | args->flags |= CREATE_NEW_CONSOLE;
        | ^~~~~~~~~~~~~~~~~~
        ../untitled/main.cpp:14:57: error: request for member ‘startupInfo’ in ‘
        args’, which is of non-class type ‘int’
        14 | args->startupInfo->dwFlags &= ~STARTF_USESTDHANDLES;
        | ^~~~~~~~~~~
        ../untitled/main.cpp:14:82: error: ‘STARTF_USESTDHANDLES’ was not declared in this scope
        14 | args->startupInfo->dwFlags &= ~STARTF_USESTDHANDLES;
        | ^~~~~~~~~~~~~~~~~~~~
        ../untitled/main.cpp:15:57: error: request for member ‘startupInfo’ in ‘* args’, which is of non-class type ‘int’
        15 | args->startupInfo->dwFlags |= STARTF_USEFILLATTRIBUTE;
        | ^~~~~~~~~~~
        ../untitled/main.cpp:15:81: error: ‘STARTF_USEFILLATTRIBUTE’ was not declared in this scope
        15 | args->startupInfo->dwFlags |= STARTF_USEFILLATTRIBUTE;
        | ^~~~~~~~~~~~~~~~~~~~~~~
        ../untitled/main.cpp:16:57: error: request for member ‘startupInfo’ in ‘* args’, which is of non-class type ‘int’
        16 | args->startupInfo->dwFillAttribute = BACKGROUND_BLUE | FOREGROUND_RED
        | ^~~~~~~~~~~
        ../untitled/main.cpp:16:88: error: ‘BACKGROUND_BLUE’ was not declared in this scope
        16 | args->startupInfo->dwFillAttribute = BACKGROUND_BLUE | FOREGROUND_RED
        | ^~~~~~~~~~~~~~~
        ../untitled/main.cpp:16:106: error: ‘FOREGROUND_RED’ was not declared in this scope
        16 | args->startupInfo->dwFillAttribute = BACKGROUND_BLUE | FOREGROUND_RED
        | ^~~~~~~~~~~~~~
        ../untitled/main.cpp:17:90: error: ‘FOREGROUND_INTENSITY’ was not declared in this scope
        17 | | FOREGROUND_INTENSITY;
        | ^~~~~~~~~~~~~~~~~~~~
        make: *** [Makefile:981: main.o] Error 1
        04:03:26: The process "/usr/bin/make" exited with code 2.
        Error while building/deploying project untitled (kit: Desktop Qt 6.2.4 GCC 64bit)
        When executing step "Make"
        04:03:26: Elapsed time: 00:01.

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by
        #3

        @JohnLocke

        Read here

        • https://doc.qt.io/qt-6/qprocess.html#setCreateProcessArgumentsModifier

        As @SGaist already said, this code will only work on Windows. I can imagine, it is also mentioned in the example.


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

        ~E. W. Dijkstra

        J 1 Reply Last reply
        0
        • Pl45m4P Pl45m4

          @JohnLocke

          Read here

          • https://doc.qt.io/qt-6/qprocess.html#setCreateProcessArgumentsModifier

          As @SGaist already said, this code will only work on Windows. I can imagine, it is also mentioned in the example.

          J Offline
          J Offline
          JohnLocke
          wrote on last edited by
          #4

          @Pl45m4 can you pls share example, it got to be much easier for me
          anything to manage terminal inside gui

          Pl45m4P 1 Reply Last reply
          0
          • J JohnLocke

            @Pl45m4 can you pls share example, it got to be much easier for me
            anything to manage terminal inside gui

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by
            #5

            @JohnLocke said in QProcess sample:

            can you pls share example

            What example?!

            anything to manage terminal inside gui

            "Terminal inside GUI"? Can you explain what you want to achieve?


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

            ~E. W. Dijkstra

            C 1 Reply Last reply
            0
            • Pl45m4P Pl45m4

              @JohnLocke said in QProcess sample:

              can you pls share example

              What example?!

              anything to manage terminal inside gui

              "Terminal inside GUI"? Can you explain what you want to achieve?

              C Offline
              C Offline
              ChrisW67
              wrote on last edited by
              #6

              The original code snippet would open an independent Windows CMD, possibly set some custom colours, change its window title, and then leave it waiting for further user input. The equivalent in the Linux world might go something like this:

              #include <QCoreApplication>
              #include <QProcess>
              #include <QTimer>
              
              int main(int argc, char **argv) {
                      QCoreApplication app(argc, argv);
              
                      QProcess process;
                      process.start("/usr/bin/xterm", 
                                      QStringList() 
                                      << "-T" << "The Child Process"
                                      << "-fg" << "red" 
                                      << "-bg" << "blue" );
              
                      QTimer::singleShot(10000, &app, &QCoreApplication::quit);
                      return app.exec();
              }
              
              JonBJ 1 Reply Last reply
              1
              • C ChrisW67

                The original code snippet would open an independent Windows CMD, possibly set some custom colours, change its window title, and then leave it waiting for further user input. The equivalent in the Linux world might go something like this:

                #include <QCoreApplication>
                #include <QProcess>
                #include <QTimer>
                
                int main(int argc, char **argv) {
                        QCoreApplication app(argc, argv);
                
                        QProcess process;
                        process.start("/usr/bin/xterm", 
                                        QStringList() 
                                        << "-T" << "The Child Process"
                                        << "-fg" << "red" 
                                        << "-bg" << "blue" );
                
                        QTimer::singleShot(10000, &app, &QCoreApplication::quit);
                        return app.exec();
                }
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @ChrisW67
                Good stuff. I assume this causes the terminal to exit after 10 seconds when the parent process exits? If so, the OP may want to use startDetached() instead of just start()?

                C 1 Reply Last reply
                0
                • JonBJ JonB

                  @ChrisW67
                  Good stuff. I assume this causes the terminal to exit after 10 seconds when the parent process exits? If so, the OP may want to use startDetached() instead of just start()?

                  C Offline
                  C Offline
                  ChrisW67
                  wrote on last edited by
                  #8

                  @JonB I just put the timer there so that the example would exit because there is no GUI other than the xterm GUI. The original code snippet would require a Ctrl-C or other forced termination. It's just an example, not a useful program.

                  J 1 Reply Last reply
                  0
                  • C ChrisW67

                    @JonB I just put the timer there so that the example would exit because there is no GUI other than the xterm GUI. The original code snippet would require a Ctrl-C or other forced termination. It's just an example, not a useful program.

                    J Offline
                    J Offline
                    JohnLocke
                    wrote on last edited by
                    #9

                    @ChrisW67 how to be able to write something inside xterm?
                    i've tried qDebug, but it sends to gnome-terminal (standard presentation)

                    Pl45m4P jsulmJ JonBJ 3 Replies Last reply
                    0
                    • J JohnLocke

                      @ChrisW67 how to be able to write something inside xterm?
                      i've tried qDebug, but it sends to gnome-terminal (standard presentation)

                      Pl45m4P Offline
                      Pl45m4P Offline
                      Pl45m4
                      wrote on last edited by
                      #10

                      @JohnLocke

                      With QProcess as shown by @ChrisW67 some answers above


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

                      ~E. W. Dijkstra

                      1 Reply Last reply
                      0
                      • J JohnLocke

                        @ChrisW67 how to be able to write something inside xterm?
                        i've tried qDebug, but it sends to gnome-terminal (standard presentation)

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @JohnLocke said in QProcess sample:

                        how to be able to write something inside xterm?

                        Using QProcess::write. There is even an example in the documentation.

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

                        1 Reply Last reply
                        1
                        • J JohnLocke

                          @ChrisW67 how to be able to write something inside xterm?
                          i've tried qDebug, but it sends to gnome-terminal (standard presentation)

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

                          @JohnLocke
                          As @jsulm says. But are you expecting anything you write to it to just appear as output in the terminal or to be interpreted as a command/input by the xterm?

                          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