QProcess freeze GUI

1.2k Views Asked by At

This is a code snippet for a QNX target. It works fine when I run it on system and when I do the next ignition cycle/ restarting my system, the GUI is freezes/hangs.

If possible, please tell me what is wrong in this code.

I tried with readAllStandardOutput and finished and started signal too with same issue. It did not help.

qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__<<__PRETTY_FUNCTION__<<!usbProcess;
usbProcess = new QProcess();
qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__<<usbProcess->pid();
usbProcess->start("usb");
qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__<<usbProcess->pid();;
usbProcess->waitForReadyRead();
qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__;
usbProcess->waitForFinished();
qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__;
text =  usbProcess->readAll();
qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__<<!usbProcess<<text;
usbProcess->closeReadChannel(QProcess::ProcessChannel::StandardOutput);
usbProcess->closeReadChannel(QProcess::ProcessChannel::StandardError);
usbProcess->closeWriteChannel();
usbProcess->close();
delete usbProcess;
qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__<<!usbProcess;
1

There are 1 best solutions below

0
On

You are running the blocking process on the GUI thread. That is why your GUI is hanging.

I usually create two methods. One which is Blocking and one which is Non-Blocking. The Blocking method is invoked using the Qt Concurrent framework by the Non-Blocking method. A signal is emitted when finished and returns any data.

// Runs the usb process without blocking
void MyClass::runUsbProcess(){
    QtConcurrent::run(this, &MyClass::runUsbProcessBlocking);
}

// Runs the usb process while blocking
void MyClass::runUsbProcessBlocking(){
    qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__<<__PRETTY_FUNCTION__<<!usbProcess;
    usbProcess = new QProcess();

    qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__<<usbProcess->pid();
    usbProcess->start("usb");

    qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__<<usbProcess->pid();;
    usbProcess->waitForReadyRead();

    qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__;
    usbProcess->waitForFinished();

    qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__;
    text =  usbProcess->readAll();
    qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__<<!usbProcess<<text;

    usbProcess->closeReadChannel(QProcess::ProcessChannel::StandardOutput);
    usbProcess->closeReadChannel(QProcess::ProcessChannel::StandardError);
    usbProcess->closeWriteChannel();
    usbProcess->close();

    delete usbProcess;
    qCDebug(SYSTEM)<<"--RDQA--"<<__LINE__<<!usbProcess;

    emit usbProcessFinished(text);
}