Random header image... Refresh for more!
Visitors

free invisible counter
Partner sites
http://consigliere.sk
--------------------------- led-svetla.sk
---------------------------
multi.xeres.cz

For liqrf project we would like to add some gui so I start thinking about possibility to have controlled existing liqrf console appliaction via some nice gui (be honest not all users like command line :) ). In this how to I’ll try to show how easily use Qprocess class for this purpose.

For Qt development I’m using Qt Creator which is freely available and combine designer and editor to one complex program.

First simple program which print some data to standard output.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
    unsigned i = 0;

    for ( ; ; ) {
        fprintf(stdout,"Value = %X\n", i++);
        fflush(stdout);
        sleep(2);
    }
    return 0;
}

For compiling use :

gcc -o test test.c

For reading stdout we use Qt signal readyStandardOutput() which is triggered when something is written to stdout.

In Qt Creator just create default Qt4 application.

Add to mainwindow.h:

private:
    Ui::MainWindowClass *ui;
    QProcess *process;

Methods and slot in mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QProcess>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindowClass)
{
    ui->setupUi(this);
    process = new QProcess(this);
    process->setReadChannel(QProcess::StandardOutput);
    connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(updateOutput()));
    process->start("./test");

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow :: updateOutput(void)
{
    QByteArray bytes = process->readAllStandardOutput();
    ui->textEdit->insertPlainText(bytes);
}

That’s all. When you run Qt application you should see all stdout in textEdit.
Complete project here:  ext_process.tar

Marek

12 Responses to “How to control console application in Qt.”

  • Haso Keric says:

    You did not declare updateOutput in the Class

  • Hi,

    it’s declared in class as public slot.

  • Danilo says:

    Hi, Marek

    I did exactly what you said, and the program test.c don’t run on my app. It just appear a window with a blank textEdit inside.

    What happened?

    Thanks

  • Hi,

    hmm maybe there’s some misunderstanding. test.c is program which write something to stdout and output from this program you will see in textEdit in QT application when you run it.
    Just run QT executable which open test executable and read from test stdout. test executable must be in same directory like qt executable. Hope I don’t confuse you ;) . If you have any problems type me an email I’ll send you code.

  • Lea says:

    Hi Marek,
    I just want to say thank you so much for this piece of code. I was nearly going mad, because I could not get my application doing this (displaying standard output in a textEdit or so). This helped me a LOT and my application is perfect now :o ) So – thank you :o )

  • Hi, good to hear you can find help from my experiments ;) . Good luck

  • srk says:

    Hi,
    I am trying to build the GUI for an executable which processes algorithms.
    I am able to start the process and give inputs. I am struck when the console application in the middle of the process looks for some input from the user.

    Is there any way which notifies QProcess or interact with console app.
    Your help would be life saver.

    Regards,
    Srk

  • Well you can use write method to push some data to console app. Code snippet will help :)

  • markc says:

    I’m not sure what to do about this in updateOutput(). Is there any chance of a complete working example?

    error: ‘class Ui::MainWindow’ has no member named ‘textEdit’

  • You can get whole qt project from here (I update post with complete example). Form with ui was missing. Sorry.

  • markc says:

    Great, thanks, now I see what I was missing. I know this goes beyond your example but if you care to offer a suggestion then it will be appreciated. If I try this it mostly works…
    m_process->start(“tail -f /var/log/syslog”);

    When quitting I get this…

    QProcess: Destroyed while process is still running.

    . is there a “proper way” to stop the background process when exiting the main program?
    . and is there a simple setting to get the window to auto scroll as new lines are added?

    If both the above are really simple and if an argv[1] could be used then this fine example actually becomes useful.

Leave a Reply