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. QProcess in different thread does not send readyRead() signal (whole concise code pasted)
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved General and Desktop
qprocessqthreadreadyread
3 Posts 2 Posters 3.2k 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.
  • Q Offline
    Q Offline
    qt_fan_4k
    wrote on last edited by qt_fan_4k
    #1

    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
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      @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
      0
      • ? A Former User

        @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 Offline
        Q Offline
        qt_fan_4k
        wrote on last edited by
        #3

        @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
        0

        • Login

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