Qt Forum

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

    Solved Problem with QProcess

    General and Desktop
    3
    11
    4963
    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.
    • Cobra91151
      Cobra91151 last edited by Cobra91151

      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 Reply Quote 0
      • ?
        A Former User last edited by

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

        Cobra91151 1 Reply Last reply Reply Quote 0
        • Cobra91151
          Cobra91151 @Guest last edited by Cobra91151

          @Wieland

          Yes, it returns true.

          process.isOpen() - returns false.

          1 Reply Last reply Reply Quote 0
          • ?
            A Former User last edited by 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.

            Cobra91151 1 Reply Last reply Reply Quote 2
            • Cobra91151
              Cobra91151 @Guest last edited by

              @Wieland

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

              1 Reply Last reply Reply Quote 0
              • ?
                A Former User last edited by 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.

                Cobra91151 2 Replies Last reply Reply Quote 3
                • Cobra91151
                  Cobra91151 @Guest last edited by Cobra91151

                  @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 Reply Quote 0
                  • Cobra91151
                    Cobra91151 @Guest last edited by

                    @Wieland

                    Maybe the problem is with connect?

                    1 Reply Last reply Reply Quote 0
                    • SGaist
                      SGaist Lifetime Qt Champion last edited by

                      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 Reply Quote 0
                      • ?
                        A Former User last edited by

                        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();
                        }
                        
                        Cobra91151 1 Reply Last reply Reply Quote 2
                        • Cobra91151
                          Cobra91151 @Guest last edited by

                          @Wieland

                          Now it works. Thanks for example code.

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