Results 1 to 6 of 6

Thread: change progress bar value from a thread

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,323
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: change progress bar value from a thread

    Try this to start:

    Qt Code:
    1. // main.cpp
    2.  
    3. #include "mainwindow.h"
    4. #include <QApplication>
    5. #include <QThread>
    6. #include <QDebug>
    7. #include <mainwindow.h>
    8.  
    9. class Thread : public QThread
    10. {
    11. Q_OBJECT
    12.  
    13. signals:
    14. void progress( int value );
    15.  
    16. private:
    17. void run()
    18. {
    19. for(int i = 0; i <= 100; i++ )
    20. {
    21. emit progress( i );
    22. QThread::sleep(1);
    23. }
    24. }
    25. };
    26.  
    27. int main(int argc, char *argv[])
    28. {
    29. QApplication a(argc, argv);
    30. MainWindow w;
    31. w.show();
    32.  
    33. qDebug()<<"From main thread: "<<QThread::currentThreadId();
    34.  
    35. Thread t;
    36. QObject::connect(&t, SIGNAL(finished()), &a, SLOT(quit()));
    37. QObject::connect(&t, SIGNAL(progress(int)), &w, SLOT(onProgress(int)));
    38.  
    39. t.start();
    40. return a.exec();
    41. }
    42.  
    43. // mainwindow.h
    44.  
    45. class MainWindow : public QMainWindow
    46. {
    47. Q_OBJECT
    48.  
    49. public:
    50. MainWindow( QWidget * parent );
    51. ~MainWindow();
    52.  
    53. public slots:
    54. void onProgress( int i );
    55.  
    56. // ...
    57. };
    58.  
    59. // mainwindow.cpp
    60.  
    61. #include "mainwindow.h"
    62. #include "ui_mainwindow.h"
    63.  
    64. MainWindow::MainWindow(QWidget *parent) :
    65. QMainWindow(parent),
    66. ui(new Ui::MainWindow)
    67. {
    68. ui->setupUi(this);
    69. }
    70.  
    71. MainWindow::~MainWindow()
    72. {
    73. delete ui;
    74. }
    75.  
    76. void MainWindow::onProgress( int i )
    77. {
    78. ui->progressBar->setValue(i);
    79. }
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  2. The following user says thank you to d_stranz for this useful post:

    xbehzad (1st December 2016)

Similar Threads

  1. Update progress bar in another thread
    By qt_developer in forum Qt Programming
    Replies: 8
    Last Post: 19th June 2012, 18:41
  2. set progress bar values inside a thread
    By ruben.rodrigues in forum Qt Programming
    Replies: 2
    Last Post: 28th May 2011, 14:55
  3. Thread updates progress bar
    By GianMarco in forum Qt Programming
    Replies: 7
    Last Post: 12th October 2009, 13:29
  4. busy progress bar without thread ?
    By npc in forum Newbie
    Replies: 34
    Last Post: 23rd July 2009, 09:29
  5. Display progress on another thread
    By radu_d in forum Qt Programming
    Replies: 1
    Last Post: 16th October 2007, 08:02

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