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

Passing a QProcess to a function

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.0k Views 1 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.
  • T Offline
    T Offline
    taraj
    wrote on last edited by
    #1

    Hi,

    How do I pass a new QProcess to a function? I can't get the pointers right... what */& goes where?

    Thank you so much!

    file.cpp (has qthreadpool to run ProcessThread, need to return value so using connect

    QProcess *proc = new QProcess();
    ProcessThread *pt = new ProcessThread(node->nodeName(), proc);
    connect(proc, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(showAnswer()),Qt::QueuedConnection);
     pool->start(pt);
    
    //showAnswer() function in file.cpp
    

    ProcessThread.cpp

    ProcessThread::ProcessThread(QString name, QProcess *proc) :  x(name), _proc(0)
    {  
       _proc = proc;
    }
    void ProcessThread::run()
    {
    }
    
    ProcessThread.h
    class ProcessThread : public QRunnable
    {
    Q_OBJECT
    public:
       ProcessThread(QString, QProcess *);
       void run();
    
    jsulmJ 1 Reply Last reply
    0
    • T taraj

      Hi,

      How do I pass a new QProcess to a function? I can't get the pointers right... what */& goes where?

      Thank you so much!

      file.cpp (has qthreadpool to run ProcessThread, need to return value so using connect

      QProcess *proc = new QProcess();
      ProcessThread *pt = new ProcessThread(node->nodeName(), proc);
      connect(proc, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(showAnswer()),Qt::QueuedConnection);
       pool->start(pt);
      
      //showAnswer() function in file.cpp
      

      ProcessThread.cpp

      ProcessThread::ProcessThread(QString name, QProcess *proc) :  x(name), _proc(0)
      {  
         _proc = proc;
      }
      void ProcessThread::run()
      {
      }
      
      ProcessThread.h
      class ProcessThread : public QRunnable
      {
      Q_OBJECT
      public:
         ProcessThread(QString, QProcess *);
         void run();
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @taraj said in Passing a QProcess to a function:

      I can't get the pointers right... what */& goes where?

      I suggest you read a book about C++ as this is really very basic knowledge.
      The code looks correct so far except I do not know how _proc is defined. Do you get any errors?

      There is another big problem with your code: if you want to use QProcess instance in another thread then either create it in that thread (inside run()) or move it to that thread using https://doc.qt.io/qt-5/qobject.html#moveToThread

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

      1 Reply Last reply
      2
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        And the next problem - you don't need a thread when you just want to run a QProcess.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        T 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          And the next problem - you don't need a thread when you just want to run a QProcess.

          T Offline
          T Offline
          taraj
          wrote on last edited by taraj
          #4

          @Christian-Ehrlicher yes, currently I have just qprocess but to run the script on a remote machine for at least 50 nodes takes too long and the rest of the GUI application 'sort of' freezes/blocks and can miss the monitoring messages it also receives separately so using threads was a way to try to speed it up
          I believe can use signals and slots to communicate back with the main thread—(using QueuedConnection) but can't quite get it going.. didn't seem to like SIGNAL(finished)

          jsulmJ Christian EhrlicherC 2 Replies Last reply
          0
          • T taraj

            @Christian-Ehrlicher yes, currently I have just qprocess but to run the script on a remote machine for at least 50 nodes takes too long and the rest of the GUI application 'sort of' freezes/blocks and can miss the monitoring messages it also receives separately so using threads was a way to try to speed it up
            I believe can use signals and slots to communicate back with the main thread—(using QueuedConnection) but can't quite get it going.. didn't seem to like SIGNAL(finished)

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

            @taraj As I wrote before: create the QProcess instance inside the thread where you want to use it...

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

            1 Reply Last reply
            0
            • T taraj

              @Christian-Ehrlicher yes, currently I have just qprocess but to run the script on a remote machine for at least 50 nodes takes too long and the rest of the GUI application 'sort of' freezes/blocks and can miss the monitoring messages it also receives separately so using threads was a way to try to speed it up
              I believe can use signals and slots to communicate back with the main thread—(using QueuedConnection) but can't quite get it going.. didn't seem to like SIGNAL(finished)

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @taraj said in Passing a QProcess to a function:

              so using threads was a way to try to speed it up

              No! QProcess is async.

              and can miss the monitoring messages

              No, this can not happen, all stdout/stderr is buffered.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              1
              • T Offline
                T Offline
                taraj
                wrote on last edited by
                #7

                I ended up doing it like this:
                https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

                QProcess was created in the thread and used signal/slot to get the return value from the QThread back to the main GUI thread using emit()

                Thanks

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Still no thread needed for such a task...

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  2

                  • Login

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