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 Update on Monday, May 27th 2025

Problem with QProcess

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 5.6k 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.
  • C Offline
    C Offline
    Cobra91151
    wrote on 16 Mar 2017, 19:51 last edited by Cobra91151
    #1

    Hi! I want to read the cmd output.

    My code:

    QProcess process;
    process.startDetached("cmd.exe", arguments);
    process.setOpenMode(QIODevice::ReadOnly);
    process.waitForFinished();
    qDebug() << process.readAllStandardOutput();
    

    The problem is I get error:
    C2248: 'QIODevice::setOpenMode': cannot access protected member declared in class 'QIODevice' , otherwise I get:
    QIODevice::read (QProcess): device not open.

    So how to fix this?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on 16 Mar 2017, 20:00 last edited by
      #2

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

      C 1 Reply Last reply 16 Mar 2017, 20:04
      0
      • ? A Former User
        16 Mar 2017, 20:00

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

        C Offline
        C Offline
        Cobra91151
        wrote on 16 Mar 2017, 20:04 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 16 Mar 2017, 20:19 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.

          C 1 Reply Last reply 16 Mar 2017, 20:27
          2
          • ? A Former User
            16 Mar 2017, 20:19

            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.

            C Offline
            C Offline
            Cobra91151
            wrote on 16 Mar 2017, 20:27 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 16 Mar 2017, 20:30 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.

              C 2 Replies Last reply 16 Mar 2017, 20:38
              3
              • ? A Former User
                16 Mar 2017, 20:30

                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.

                C Offline
                C Offline
                Cobra91151
                wrote on 16 Mar 2017, 20:38 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
                  16 Mar 2017, 20:30

                  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.

                  C Offline
                  C Offline
                  Cobra91151
                  wrote on 16 Mar 2017, 21:25 last edited by
                  #8

                  @Wieland

                  Maybe the problem is with connect?

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 16 Mar 2017, 21:41 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 16 Mar 2017, 21:52 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();
                      }
                      
                      C 1 Reply Last reply 16 Mar 2017, 22:27
                      2
                      • ? A Former User
                        16 Mar 2017, 21:52

                        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();
                        }
                        
                        C Offline
                        C Offline
                        Cobra91151
                        wrote on 16 Mar 2017, 22:27 last edited by
                        #11

                        @Wieland

                        Now it works. Thanks for example code.

                        1 Reply Last reply
                        0

                        3/11

                        16 Mar 2017, 20:04

                        8 unread
                        • Login

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