Results 1 to 11 of 11

Thread: Implementing a ProgressBar using QProcess

  1. #1
    Join Date
    Aug 2011
    Posts
    15
    Thanks
    1
    Qt products
    Qt3
    Platforms
    MacOS X

    Thumbs up Implementing a ProgressBar using QProcess

    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:

    Qt Code:
    1. #include "progres.h"
    2. #include <QProcess>
    3. #include <QProgressBar>
    4.  
    5. Progres ::Progres(QWidget *parent)
    6. {
    7. progressbar = new QProgressBar(this);
    8. progressbar->setGeometry(75,40,200,35);
    9. progressbar->setRange(0,100);
    10. proces = new QProcess(this);
    11. proces->setReadChannelMode(QProcess::MergedChannels);
    12. proces->setReadChannel(QProcess::StandardOutput);
    13. connect(proces, SIGNAL(readyReadStandardOutput()),this, SLOT(disp()));
    14. proces->start("./tstf");
    15. }
    16. void Progres :: disp()
    17. {
    18. int t;
    19. t = proces->readAllStandardOutput().toInt();
    20. progressbar->setValue(t);
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 

    Where "tstf" is a C obj file that i've copied in the dir of my Qt project.
    Last edited by wysota; 13th August 2011 at 08:11. Reason: missing [code] tags

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Implementing a ProgressBar using QProcess

    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).

  3. #3
    Join Date
    Aug 2011
    Posts
    15
    Thanks
    1
    Qt products
    Qt3
    Platforms
    MacOS X

    Thumbs up Re: Implementing a ProgressBar using QProcess

    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.....

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Implementing a ProgressBar using QProcess

    You can write to your C app using one of QIODevice::write methods, and you will need to implement reading from stdin in the C program.

  5. #5
    Join Date
    Aug 2011
    Posts
    15
    Thanks
    1
    Qt products
    Qt3
    Platforms
    MacOS X

    Default Re: Implementing a ProgressBar using QProcess

    thanks for your guidance.. I'll try it and will reply you...

  6. #6
    Join Date
    Aug 2011
    Posts
    15
    Thanks
    1
    Qt products
    Qt3
    Platforms
    MacOS X

    Thumbs up Re: Implementing a ProgressBar using QProcess

    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..

    Qt Code:
    1. #include<stdio.h>
    2. void main()
    3. {
    4. int i;
    5. for(i=0;i<101;i++)
    6. {
    7. printf("\n%d",i);
    8. fflush(stdout);
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 15th August 2011 at 14:50. Reason: missing [code] tags

  7. #7
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Implementing a ProgressBar using QProcess

    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:
    Qt Code:
    1. printf("\n%d",i);
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. printf("%d\n",i);
    To copy to clipboard, switch view to plain text mode 
    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 ?
    Last edited by stampede; 15th August 2011 at 12:44.

  8. #8
    Join Date
    Aug 2011
    Posts
    15
    Thanks
    1
    Qt products
    Qt3
    Platforms
    MacOS X

    Thumbs up Re: Implementing a ProgressBar using QProcess

    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 .

    Qt Code:
    1. #include<stdio.h>
    2. #include<stdlib.h>
    3. void main()
    4. {
    5. int i;
    6. for(i=0;i<101;i++)
    7. {
    8. fflush(stdout);
    9. printf("%d",i);
    10. fflush(stdout);
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by wysota; 15th August 2011 at 14:51. Reason: missing [code] tags

  9. #9
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Implementing a ProgressBar using QProcess

    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):
    Qt Code:
    1. void Progres :: disp()
    2. {
    3. while( 1 ){
    4. QString t = process->readLine();
    5. if( t.isEmpty() ) break;
    6. qDebug() << "got output" << t;
    7. progressBar->setValue(t.toInt());
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Aug 2011
    Posts
    15
    Thanks
    1
    Qt products
    Qt3
    Platforms
    MacOS X

    Thumbs up Re: Implementing a ProgressBar using QProcess

    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...

  11. #11
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Implementing a ProgressBar using QProcess

    No prob, good that it works now.

Similar Threads

  1. Confused with progressbar
    By hakermania in forum Newbie
    Replies: 2
    Last Post: 23rd January 2011, 10:47
  2. Replies: 7
    Last Post: 1st March 2010, 17:49
  3. Indefinite ProgressBar
    By csvivek in forum Qt Programming
    Replies: 2
    Last Post: 15th April 2008, 11:02
  4. how to use progressBar in simple program
    By jyoti in forum Newbie
    Replies: 1
    Last Post: 1st December 2006, 12:16
  5. Progressbar problem
    By thae in forum Qt Programming
    Replies: 4
    Last Post: 4th November 2006, 11:48

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.