Inter-process communication in QT using qprocess

Source: Internet
Author: User

Recently, I needed to call a Cui program from within QT GUI window and simulate the effect of an embedded command line window like the Autolisp console in AutoCAD. at the beginning, I wanted to implement this function using UNIX pipe. however, I found out there isQprocessClass, which brings more convenience. The procedures are obvious and shown as follows:

# Create QProcess objectcb = new Qprocess();# Start a child process with the inter-process communication mode set to ReadWrite and Unbufferedcb->start("./abbcbctrl", QStringList()<<"-g", QIODevice::ReadWrite | QIODevice::Unbuffered);# Connect signals with related slots: readyReadStandardOutput is emitted when the standard output buffer is ready for reading while readyReadStandardError is related to the standard errorconnect(cb, SIGNAL(readyReadStandardOutput()), this, SLOT(DumpStdOut()));connect(cb, SIGNAL(readyReadStandardError()), this, SLOT(DumpStdErr()));

The slot functions are as follows:

void MainWindow::DumpStdOut() {    QByteArray strdata = cb->readAllStandardOutput();    ui->txtCUI->setTextColor(Qt::green);    ui->txtCUI->append(strdata);}void MainWindow::DumpStdErr() {    QByteArray strdata = cb->readAllStandardError();    ui->txtCUI->setTextColor(Qt::red);    ui->txtCUI->append(strdata);}

When running the above program, it was strange to discover that the message sent from the child process couldn't be displayed immediately in the qt gui. only when an enough quantity of characters arrived, they cocould be displayed. at first, I thought the problem might be asynchronous communication mode was used and neither the standard output nor the standard error buffer size cocould be changed. therefore, I tried to used synchronous mode to read the output from child process periodically. however, this method caused the GUI interface did not response anymore. on the next day, an idea came to my mind that the problem might come from the child process, in which I did not callFflushFunction after usingPrintf.Here, the functionFflushMeans enforce all the data exiting in the buffer to be dumped. if we directly run a Cui program in the terminal, the display of the characters is OK. this is because in the terminal, the string buffering is based upon the line ending character "\ n ". whenever it meets "\ n", it will automatically flush the buffer. however, when a child process sends information through pipe toQprocessObject in QT, the buffering mechanic is different, I. e. Only when the predefined buffer is full, the characters will then be actually output. The problem has finally been solved after addingFflushFunction.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.