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

Problem with QProcess

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 5.7k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #2

    Hi! Does process.startDetached(...) return true?

    Cobra91151C 1 Reply Last reply
    0
    • ? A Former User

      Hi! Does process.startDetached(...) return true?

      Cobra91151C Offline
      Cobra91151C Offline
      Cobra91151
      wrote on last edited by Cobra91151
      #3

      @Wieland

      Yes, it returns true.

      process.isOpen() - returns false.

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by A Former User
        #4

        Good. That means that "cmd.exe" has been started successfully. But QProcess::startDetached() is static, so it's not related to your process object. In other words: process has not been started. To start process, call the non-static process.start(...) . You can't use QProcess::readAllStandardOutput together with a detached process.

        Cobra91151C 1 Reply Last reply
        2
        • ? A Former User

          Good. That means that "cmd.exe" has been started successfully. But QProcess::startDetached() is static, so it's not related to your process object. In other words: process has not been started. To start process, call the non-static process.start(...) . You can't use QProcess::readAllStandardOutput together with a detached process.

          Cobra91151C Offline
          Cobra91151C Offline
          Cobra91151
          wrote on last edited by
          #5

          @Wieland

          I have tried process.start("cmd.exe", arguments); but it freezes my application window?

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by A Former User
            #6

            Sure it freezes your application, because you're blocking the GUI thread with waitForFinished until "cmd.exe" terminates.

            Connect to the signals started and finished instead.

            Cobra91151C 2 Replies Last reply
            3
            • ? A Former User

              Sure it freezes your application, because you're blocking the GUI thread with waitForFinished until "cmd.exe" terminates.

              Connect to the signals started and finished instead.

              Cobra91151C Offline
              Cobra91151C Offline
              Cobra91151
              wrote on last edited by Cobra91151
              #7

              @Wieland

              Ok. I will fix and check it. Thanks.

              I have added started signal:

              QProcess *process = new QProcess();
              connect(process, &QProcess::started, this, &TestSettings::test);
              process->start("cmd.exe", arguments);
              
              void TestSettings::test()
              {
                   qDebug() << process->readAllStandardOutput();
              }
              

              But nothing outputs?

              1 Reply Last reply
              0
              • ? A Former User

                Sure it freezes your application, because you're blocking the GUI thread with waitForFinished until "cmd.exe" terminates.

                Connect to the signals started and finished instead.

                Cobra91151C Offline
                Cobra91151C Offline
                Cobra91151
                wrote on last edited by
                #8

                @Wieland

                Maybe the problem is with connect?

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

                  You should connect the readyReadStandardOutput and the readyReadStandardError signals if you want to see something when your command runs.

                  The started signal only tells you that the process successfully started, that doesn't mean it has written anything on the standard output or error channel.

                  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
                  0
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #10

                    Hi, look:

                    main.cpp

                    #include <QApplication>
                    #include "myclass.h"
                    
                    int main(int argc, char *argv[])
                    {
                        QApplication a(argc, argv);
                        MyClass mc;
                        QObject::connect(&mc, &MyClass::completed, &a, &QApplication::quit);
                        mc.begin();
                        return a.exec();
                    }
                    

                    myclass.h

                    #ifndef MYCLASS_H
                    #define MYCLASS_H
                    
                    #include <QObject>
                    #include <QProcess>
                    
                    class MyClass : public QObject
                    {
                        Q_OBJECT
                    public:
                        explicit MyClass(QObject *parent = nullptr);
                    
                    signals:
                        void completed();
                    
                    public slots:
                        void begin();
                    
                    private slots:
                        void onStarted();
                        void onErrorOccurred(QProcess::ProcessError error);
                        void onFinished(int exitCode, QProcess::ExitStatus exitStatus);
                        void onReadyReadStandardOutput();
                    
                    private:
                        QProcess m_process;
                    };
                    
                    #endif // MYCLASS_H
                    

                    myclass.cpp

                    #include "myclass.h"
                    #include <Qtimer>
                    #include <QDebug>
                    
                    MyClass::MyClass(QObject *parent)
                        : QObject(parent)
                    {
                        connect(&m_process, &QProcess::started, this, &MyClass::onStarted);
                        connect(&m_process, &QProcess::errorOccurred, this, &MyClass::onErrorOccurred);
                        connect(&m_process, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
                                this, &MyClass::onFinished);
                        connect(&m_process, &QProcess::readyReadStandardOutput, this, &MyClass::onReadyReadStandardOutput);
                    }
                    
                    void MyClass::begin()
                    {
                        m_process.start("cmd.exe", QStringList());
                    }
                    
                    void MyClass::onStarted()
                    {
                        qDebug() << "Started";
                        QTimer::singleShot(2000, &m_process, &QProcess::kill);
                    }
                    
                    void MyClass::onErrorOccurred(QProcess::ProcessError error)
                    {
                        qDebug() << QString("Error occurred, error=%1").arg(static_cast<int>(error));
                    }
                    
                    void MyClass::onFinished(int exitCode, QProcess::ExitStatus exitStatus)
                    {
                        qDebug() << QString("Finished, exitCode=%1, exitStatus=%2").arg(static_cast<int>(exitCode))
                                    .arg(static_cast<int>(exitStatus));
                        emit completed();
                    }
                    
                    void MyClass::onReadyReadStandardOutput()
                    {
                        qDebug() << m_process.readAllStandardOutput();
                    }
                    
                    Cobra91151C 1 Reply Last reply
                    2
                    • ? A Former User

                      Hi, look:

                      main.cpp

                      #include <QApplication>
                      #include "myclass.h"
                      
                      int main(int argc, char *argv[])
                      {
                          QApplication a(argc, argv);
                          MyClass mc;
                          QObject::connect(&mc, &MyClass::completed, &a, &QApplication::quit);
                          mc.begin();
                          return a.exec();
                      }
                      

                      myclass.h

                      #ifndef MYCLASS_H
                      #define MYCLASS_H
                      
                      #include <QObject>
                      #include <QProcess>
                      
                      class MyClass : public QObject
                      {
                          Q_OBJECT
                      public:
                          explicit MyClass(QObject *parent = nullptr);
                      
                      signals:
                          void completed();
                      
                      public slots:
                          void begin();
                      
                      private slots:
                          void onStarted();
                          void onErrorOccurred(QProcess::ProcessError error);
                          void onFinished(int exitCode, QProcess::ExitStatus exitStatus);
                          void onReadyReadStandardOutput();
                      
                      private:
                          QProcess m_process;
                      };
                      
                      #endif // MYCLASS_H
                      

                      myclass.cpp

                      #include "myclass.h"
                      #include <Qtimer>
                      #include <QDebug>
                      
                      MyClass::MyClass(QObject *parent)
                          : QObject(parent)
                      {
                          connect(&m_process, &QProcess::started, this, &MyClass::onStarted);
                          connect(&m_process, &QProcess::errorOccurred, this, &MyClass::onErrorOccurred);
                          connect(&m_process, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
                                  this, &MyClass::onFinished);
                          connect(&m_process, &QProcess::readyReadStandardOutput, this, &MyClass::onReadyReadStandardOutput);
                      }
                      
                      void MyClass::begin()
                      {
                          m_process.start("cmd.exe", QStringList());
                      }
                      
                      void MyClass::onStarted()
                      {
                          qDebug() << "Started";
                          QTimer::singleShot(2000, &m_process, &QProcess::kill);
                      }
                      
                      void MyClass::onErrorOccurred(QProcess::ProcessError error)
                      {
                          qDebug() << QString("Error occurred, error=%1").arg(static_cast<int>(error));
                      }
                      
                      void MyClass::onFinished(int exitCode, QProcess::ExitStatus exitStatus)
                      {
                          qDebug() << QString("Finished, exitCode=%1, exitStatus=%2").arg(static_cast<int>(exitCode))
                                      .arg(static_cast<int>(exitStatus));
                          emit completed();
                      }
                      
                      void MyClass::onReadyReadStandardOutput()
                      {
                          qDebug() << m_process.readAllStandardOutput();
                      }
                      
                      Cobra91151C Offline
                      Cobra91151C Offline
                      Cobra91151
                      wrote on last edited by
                      #11

                      @Wieland

                      Now it works. Thanks for example code.

                      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