Results 1 to 6 of 6

Thread: change progress bar value from a thread

  1. #1
    Join Date
    Nov 2016
    Posts
    3
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default change progress bar value from a thread

    hi every one.
    i write this cod.
    how i can change progress bar value from my thread?
    signal and slots?
    how use signal and slots for this program??


    main.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3. #include <QThread>
    4. #include <QDebug>
    5. #include <mainwindow.h>
    6.  
    7.  
    8.  
    9.  
    10. int i ;
    11.  
    12.  
    13.  
    14.  
    15.  
    16.  
    17.  
    18.  
    19.  
    20.  
    21.  
    22.  
    23.  
    24. class Thread : public QThread
    25. {
    26. private:
    27. void run()
    28. {
    29. for(i;i<=100;i++)
    30. QThread::sleep(1);
    31.  
    32. }
    33.  
    34. };
    35.  
    36.  
    37.  
    38.  
    39.  
    40. int main(int argc, char *argv[])
    41. {
    42. QApplication a(argc, argv);
    43. MainWindow w;
    44. w.show();
    45.  
    46.  
    47.  
    48. //Thread();
    49.  
    50.  
    51.  
    52.  
    53.  
    54. qDebug()<<"From main thread: "<<QThread::currentThreadId();
    55.  
    56. Thread t;
    57. QObject::connect(&t, SIGNAL(finished()), &a, SLOT(quit()));
    58.  
    59. t.start();
    60.  
    61.  
    62. //--------------------
    63.  
    64.  
    65. //---------------------------------------
    66.  
    67.  
    68.  
    69.  
    70.  
    71.  
    72.  
    73.  
    74. return a.exec();
    75.  
    76. }
    To copy to clipboard, switch view to plain text mode 




    mainwindow.cpp:

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QApplication>
    4.  
    5.  
    6.  
    7.  
    8.  
    9. extern int i;
    10.  
    11.  
    12.  
    13.  
    14.  
    15.  
    16. MainWindow::MainWindow(QWidget *parent) :
    17. QMainWindow(parent),
    18. ui(new Ui::MainWindow)
    19. {
    20. ui->setupUi(this);
    21.  
    22.  
    23. }
    24.  
    25. MainWindow::~MainWindow()
    26. {
    27. delete ui;
    28.  
    29. }
    30.  
    31. void MainWindow::on_pushButton_pressed()
    32. {
    33.  
    34. // ui->progressBar->setValue(i);
    35.  
    36.  
    37. //ui->progressBar->update();
    38.  
    39. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 29th November 2016 at 13:21. Reason: changed [qtclass] to [code]

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: change progress bar value from a thread

    Yes, you can add a signal to your thread class and emit it whenever the thread needs to send new progress info.
    Connect that to a slot in your main window.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    xbehzad (29th November 2016)

  4. #3
    Join Date
    Nov 2016
    Posts
    3
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: change progress bar value from a thread

    Thnak you cheers. but how i write a connection for this program?
    please give me a simple cod

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: change progress bar value from a thread

    The Qt documentation will teach you all about signals, slots, and connections.
    <=== 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.

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

    xbehzad (30th November 2016)

  7. #5
    Join Date
    Nov 2016
    Posts
    3
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: change progress bar value from a thread

    Thank you m freinds. im in learning to write connection. i can write simple connection but for this program i thik much time and nothing come to my mind.
    please give me a simple connect for this program

  8. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 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.

  9. 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, 19:41
  2. set progress bar values inside a thread
    By ruben.rodrigues in forum Qt Programming
    Replies: 2
    Last Post: 28th May 2011, 15:55
  3. Thread updates progress bar
    By GianMarco in forum Qt Programming
    Replies: 7
    Last Post: 12th October 2009, 14:29
  4. busy progress bar without thread ?
    By npc in forum Newbie
    Replies: 34
    Last Post: 23rd July 2009, 10:29
  5. Display progress on another thread
    By radu_d in forum Qt Programming
    Replies: 1
    Last Post: 16th October 2007, 09: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.