Random header image... Refresh for more!
opensuse 11.3
Visitors

free invisible counter
Partner sites
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);
}

Thats all. When you run Qt application you should see all stdout in textEdit.

Marek

6 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

Leave a Reply