Results 1 to 6 of 6

Thread: Need some help about QProgressbar while using QThread

  1. #1

    Default Need some help about QProgressbar while using QThread

    My application is to do some file operations.You know while processing file of large size ,the application looks as if it is dead.So I put all my works into QThread like this:
    Qt Code:
    1. class MainOperation:public QThread
    2. {
    3. Q_OBJECT
    4. ....
    5. public slots:
    6. virtual void run()
    7. {
    8. FileWriter fw1(_errorFile);
    9. FileReviser fr(_templateFile);
    10. fw1.appendReviser(&fr);
    11. emit fileSizedRecognized(fw1.GetFileSize());
    12. while(!fw1.IsOK())
    13. {
    14. fw1.save(_outputFile);
    15. emit posChanged(fw1.GetPos());
    16. }
    17. emit finished();
    18. }
    19. signals:
    20. void fileSizedRecognized(int fileSize);
    21. void posChanged(int pos);
    22. void finished();
    23. }
    To copy to clipboard, switch view to plain text mode 
    Then I want these signals to be sent to My UI Form then update the position of the progressbar like this(below are slots in my UI Form):
    Qt Code:
    1. public slots:
    2. void prepareFile(int fileSize)
    3. {
    4. pushButton_Begin->setEnabled(false);
    5. pushButton_end->setEnabled(false);
    6. _fileSize=fileSize;
    7. progressBar->setTotalSteps(100);
    8. }
    9. void setProgressBarPos(int pos)
    10. {
    11. progressBar->setProgress(pos/_fileSize*100);
    12. }
    13. void finish()
    14. {
    15. pushButton_end->setEnabled(true);
    16. }
    To copy to clipboard, switch view to plain text mode 
    But it doesn't work.These slots are fired but the ProgressBar's position is always zero.Below is code fragments in main.cpp
    Qt Code:
    1. Ui::Form ui;
    2. ui.setupUi(&w);
    3. ui.label_ErrorFile->setText(sErrorFile);
    4. ui.label_TemplateFile->setText(sTemplateFile);
    5. ui.label_OutputFile->setText(sOutputFile);
    6. MainOperation mainOp(sErrorFile,sTemplateFile,sOutputFile);
    7. QObject::connect(ui.pushButton_Begin,SIGNAL(clicked()),&mainOp,SLOT(start()
    8. ));
    9. QObject::connect(&mainOp,SIGNAL(fileSizedRecognized(int)),&ui,SLOT(prepareF
    10. ile(int)));
    11. QObject::connect(&mainOp,SIGNAL(posChanged(int)),&ui,SLOT(setProgressBarPos
    12. (int)));
    13. QObject::connect(&mainOp,SIGNAL(finished()),&ui,SLOT(finish()));
    14. QObject::connect(ui.pushButton_end, SIGNAL(clicked()), &app, SLOT(quit()));
    To copy to clipboard, switch view to plain text mode 
    I was really confused,expecting your help.Thanks!

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need some help about QProgressbar while using QThread

    Have you edited by any chance the code generated by UIC?
    For example, the UI::Form class. This is not ok, because it will get overwritten each time you modify the ui file.

    Also, why are you using Q3ProgressBar?

    My guess is that the progress bar always is at 0 because the file pos always comes 0, therefore pos/_fielSize*100 = 0;

    Are you sure all values are ok?

    Regards

  3. #3

    Default Re: Need some help about QProgressbar while using QThread

    Thank you for your replay.
    I have done something to debug my code,it did work well but the postion of progress bar did not change.It looks as if the event is not fired until the thread ends.I have ever used QProgressbar,while Q3Progressbar is my secondary chioce.
    I have also tried app->processEvents every time the MainObject emit the signal,but it is of no help.Maybe QCustomEvent is the only way?

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need some help about QProgressbar while using QThread

    Another thing: in QThread, run is not a public slot.
    run() is virtual protected in QThread so you should defined as such.


    The code look OK, although you should choose one of the "standard" coding styles, i.e. implement the methods in cpp files, not inline( this is more like java ).

    Try switching the visibility of run. That could have been the problem.
    Emitting a signal from a thread is OK, no need for custom events.

    Regards

  5. #5

    Default Re: Need some help about QProgressbar while using QThread

    Now everything gose well.Besides the visibility problem ,there is another mistake.That is the signal should be sent to the progressbar directly.I sent the signal to my UI Form and the UI Form call setvalue() or setprogress() to change the position of progressbar.This is a invalid operation.
    Thank you,marcel!

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need some help about QProgressbar while using QThread

    Quote Originally Posted by qzb1111 View Post
    Now everything gose well.Besides the visibility problem ,there is another mistake.That is the signal should be sent to the progressbar directly.I sent the signal to my UI Form and the UI Form call setvalue() or setprogress() to change the position of progressbar.This is a invalid operation.
    Thank you,marcel!
    You're welcome, but having the form class as proxy is not a bad idea so you should stick to it.

    Although the signal is posted as an event for the progress bar, worker threads shouldn't work directly with widgets.

    Regards

Similar Threads

  1. QThread exec proplem to stop...
    By patrik08 in forum Qt Programming
    Replies: 29
    Last Post: 21st May 2007, 07:51
  2. Qthread Issue?
    By vishal.chauhan in forum Newbie
    Replies: 3
    Last Post: 29th March 2007, 08:50
  3. QGraphicsScene and QThread
    By tts80 in forum Qt Programming
    Replies: 5
    Last Post: 10th January 2007, 09:32
  4. how to use QHttp inside QThread in Qt3
    By alusuel in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2006, 11:19
  5. Is it possible to create a QThread without inheriting ?
    By probine in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2006, 22:51

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
  •  
Qt is a trademark of The Qt Company.