PDA

View Full Version : Implementing a ProgressBar using QProcess



nitish
12th August 2011, 14:04
Hi there i've written a C code which prints the integer values 1 to 50 and i tried to change my progress bar dynamically by using QProcess. But the progress bar is not varying. If my c file has only one printf that prints any value for example 50, then the progress bar works, but whenever i try to loop some values i cannot get the variation in the progress bar. I just want to know how to do it using QProcess. Can anyone help me regarding this issue.....? Thank you.... Here's my code:


#include "progres.h"
#include <QProcess>
#include <QProgressBar>

Progres ::Progres(QWidget *parent)
{
progressbar = new QProgressBar(this);
progressbar->setGeometry(75,40,200,35);
progressbar->setRange(0,100);
proces = new QProcess(this);
proces->setReadChannelMode(QProcess::MergedChannels);
proces->setReadChannel(QProcess::StandardOutput);
connect(proces, SIGNAL(readyReadStandardOutput()),this, SLOT(disp()));
proces->start("./tstf");
}
void Progres :: disp()
{
int t;
t = proces->readAllStandardOutput().toInt();
progressbar->setValue(t);

}

Where "tstf" is a C obj file that i've copied in the dir of my Qt project.

stampede
12th August 2011, 15:36
My bet is that the printf() messages are buffered, so this is not a problem with QProcess but with your C app.
You can add fflush(stdout) after printf() to your C code, it should work then (you'll receive readyReadStandardOutput() signals from QProcess after every fflush call).

nitish
13th August 2011, 08:05
Hi thanks for your solution, now my program works pretty good... and now i've another issue regarding the signals and slot for the QProgress Bar, now i need to send the signals from my Qt program to a C file and process further using those values. I mean the reverse of what i did previously. For instance if i change the the slider of my progress bar in Qt, the values in the C file should change accordingly. Thanks in advance.....

stampede
13th August 2011, 08:49
You can write to your C app using one of QIODevice::write (http://doc.qt.nokia.com/latest/qiodevice.html#write)methods, and you will need to implement reading from stdin in the C program.

nitish
13th August 2011, 08:53
thanks for your guidance.. I'll try it and will reply you...

nitish
15th August 2011, 12:55
Hi there, i have tried my progress bar with the following C code to print 100 numbers so that i could process it with Qprocess, yet my progress bar shows no variation... Can u resolve this issue.. Thanks..


#include<stdio.h>
void main()
{
int i;
for(i=0;i<101;i++)
{
printf("\n%d",i);
fflush(stdout);
}
}

stampede
15th August 2011, 13:04
Can u resolve this issue.
Why should I ? I can only give you some help, I wont do the job for you.

, yet my progress bar shows no variation.
Is the "readyReadStandardOutput" signal emitted ? What can you read from the process ?
I dont remember how toInt() will work if the new line character is at the beggining, so maybe change this:

printf("\n%d",i);
to

printf("%d\n",i);
And provide more description for your problem than "it wont work, can you fix it".

----
edit:
few posts above you said "now my program works pretty good...", so what happened ?

nitish
15th August 2011, 13:47
I'm really sorry i didn't mean to have my job done by you. And thanks a lot for your help and spending out your time.
A signal is emitted from "readyReadStandardOutput" and when i "qDebug" the process i can get only the first value i.e; "0" in the output .


#include<stdio.h>
#include<stdlib.h>
void main()
{
int i;
for(i=0;i<101;i++)
{
fflush(stdout);
printf("%d",i);
fflush(stdout);
}
}

this is my C code that prints the values, and i tried to process it with QProcess but my output shows only the first value. And i hope that i would get some help from you.. Thanks a lot.

stampede
15th August 2011, 14:31
Can you verify that all numbers are not available in "disp" slot at once ? Put a qDebug() statement there to see what "readAllStandardOutput" returns, it could happen that your C app writes the numbers so fast that between emitting first "readyReadStdOut" signal and "readAllStdOut" call all numbers are already in stdout.
If this is the case, you can call "readLine" instead (in C app print one number in line):


void Progres :: disp()
{
while( 1 ){
QString t = process->readLine();
if( t.isEmpty() ) break;
qDebug() << "got output" << t;
progressBar->setValue(t.toInt());
}
}

nitish
15th August 2011, 14:52
Thanks a lot for your help..... That was what i actually needed.. It works fine now..... I'm just a beginner to Qt that's why i couldn't trace out many new things.. Thanks once again for your help...

stampede
15th August 2011, 15:37
No prob, good that it works now.