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 use QProcess in C++ class
Qt 6.11 is out! See what's new in the release blog

How use QProcess in C++ class

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 838 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.
  • R Offline
    R Offline
    realroot
    wrote on last edited by
    #1

    Is this a correct way to handle a QProcess as member of a class?
    I think that I should do this in this case instead of creating one every time that I call the function that uses it.

    class MyClass : public QAbstractListModel {
        Q_OBJECT
    
    public:
        Project(QObject *parent = nullptr) : QAbstractListModel(parent),
            m_manager(new QNetworkAccessManager(this)), m_process(nullptr) {
            connect(m_manager, &QNetworkAccessManager::finished,
                    this, &Project::downloadFinished);
        }
    
    ...
    
    private:
        QVector<Data> m_data;
        QNetworkAccessManager* m_manager;
        QProcess* m_process;
    };
    

    In one function called from QML I start a process:

    void MyClass::doSomething(const QString& cmd, const QStringList& cmdArgs) {
        m_process = new QProcess(this);
        m_process->start(cmd, cmdArgs);
    }
    

    I did not test this code because until now I was creating a QProcess directly in the function.

    When I call the function I do not care to what happen when the process ends.
    I'd like to check if the process is already running in case the function is called quickly more times.
    In that case the already running process should be stopped before running the new one.

    And I do not want the function to block QML and the rest of the application.
    Therefore should I use QProcess::setProgram, QProcess::setArguments and QProcess::startDetached?

    JonBJ 1 Reply Last reply
    0
    • R realroot

      Is this a correct way to handle a QProcess as member of a class?
      I think that I should do this in this case instead of creating one every time that I call the function that uses it.

      class MyClass : public QAbstractListModel {
          Q_OBJECT
      
      public:
          Project(QObject *parent = nullptr) : QAbstractListModel(parent),
              m_manager(new QNetworkAccessManager(this)), m_process(nullptr) {
              connect(m_manager, &QNetworkAccessManager::finished,
                      this, &Project::downloadFinished);
          }
      
      ...
      
      private:
          QVector<Data> m_data;
          QNetworkAccessManager* m_manager;
          QProcess* m_process;
      };
      

      In one function called from QML I start a process:

      void MyClass::doSomething(const QString& cmd, const QStringList& cmdArgs) {
          m_process = new QProcess(this);
          m_process->start(cmd, cmdArgs);
      }
      

      I did not test this code because until now I was creating a QProcess directly in the function.

      When I call the function I do not care to what happen when the process ends.
      I'd like to check if the process is already running in case the function is called quickly more times.
      In that case the already running process should be stopped before running the new one.

      And I do not want the function to block QML and the rest of the application.
      Therefore should I use QProcess::setProgram, QProcess::setArguments and QProcess::startDetached?

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @realroot
      You create and leak a new QProcess instance each time doSomething() is called.

      You sound like you want startDetached(). I don't know whether the QProcess then tells you whether it's still running.

      in case the function is called quickly more times

      You can sort that out from code.

      "Stopping" running programs means killing them. This is generally not a great idea.

      You could perhaps still use start(). I think you'll need to keep a QList<QProcess *> since you want to run multiple processes at a time (right?) and you still want the handles to the running ones.

      R 1 Reply Last reply
      1
      • JonBJ JonB

        @realroot
        You create and leak a new QProcess instance each time doSomething() is called.

        You sound like you want startDetached(). I don't know whether the QProcess then tells you whether it's still running.

        in case the function is called quickly more times

        You can sort that out from code.

        "Stopping" running programs means killing them. This is generally not a great idea.

        You could perhaps still use start(). I think you'll need to keep a QList<QProcess *> since you want to run multiple processes at a time (right?) and you still want the handles to the running ones.

        R Offline
        R Offline
        realroot
        wrote on last edited by
        #3

        Using startDetached() application flow is not interrupted.

        void MyClass::doSomething(const QString& cmd, const QStringList& cmdArgs) {
            QProcess process;
            process.setProgram(cmd);
            process.setArguments(cmdArgs);
            process.startDetached();
        }
        

        Actually I think that that there is no need to handle them at least for now.

        1 Reply Last reply
        0
        • R realroot has marked this topic as solved on

        • Login

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