Qt Forum

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

    Update: Forum Guidelines & Code of Conduct

    Solved QProcess in different thread does not send readyRead() signal (whole concise code pasted)

    General and Desktop
    qprocess qthread readyread
    2
    3
    2817
    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.
    • Q
      qt_fan_4k last edited by qt_fan_4k

      I have a worker object in separated thread, launching QProcess command within this same thread, but the slot processCommandOutput is never triggered.

      So the question is : it looks the readyRead() signal is never triggered, but if I call worker in the same thread of MainWindow, everything is fine.

      When I replaced the 'start' with 'execute' for the QProcess object workProcess, I can see the command output in the IDE command output window, e.g., Qt Creator Application Output.

      I don't see any error in this multi-thread programming because it is so short and straightforward, and I don't see any warning message in my Qt Creator.

      I'm using Qt4.8.

      void MainWindow::processInputCommand() {
          activeWorker->setCommand(cmdView->text().trimmed());
      
          // Give QThread ownership of Worker Object
          QThread *thread = new QThread;
          activeWorker->moveToThread(thread);
      
          // Connects the thread’s started() signal to the process() slot in the worker, causing it to start.
          connect(thread, SIGNAL(started()), activeWorker, SLOT(process()));
      
          thread->start();
      }
      
      Worker::Worker(QObject *parent) :
          QObject(parent)
      {
          workProcess = new QProcess(this);
          workProcess->setProcessChannelMode(QProcess::MergedChannels);
      }
      
      void Worker::setCommand(const QString &cmd) {
          cmdToRun = cmd;
      }
      
      void Worker::process() {
          connect(workProcess, SIGNAL(readyRead()), this, SLOT(processCommandOutput()));
          workProcess->start(cmdToRun);          // if I use execute here, I can see command output in QtCreator Application Output
          emit finished();
      }
      
      void Worker::processCommandOutput() {
          qDebug() << "GOGO";
          QString sz = QString(workProcess->readAllStandardOutput());
          emit outputDataReady(sz);
      }
      
      
      1 Reply Last reply Reply Quote 0
      • ?
        A Former User last edited by

        @qt_fan_4k Hi!
        QProcess::start() starts the process asynchronously. So in the following snippet...

        workProcess->start(cmdToRun); 
        emit finished();
        

        The finished() signal is emitted immediately after the start() call and thus it depends only on your luck if the started process 1) has already been started and 2) has already finished. See: http://doc.qt.io/qt-5/qprocess.html#start

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

          @Wieland
          Thank you so much, Wieland. You're 100% right, and I was not lucky enough :)
          After moving the sending of finish signal elsewhere, everything goes fine.

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