Results 1 to 5 of 5

Thread: Control MainWindow from QThread

  1. #1
    Join Date
    Sep 2008
    Posts
    60
    Thanks
    20
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Control MainWindow from QThread

    Hi everybody,

    I would like to know the best way to control/modify elements of my MainWindow class from a QThread class started from the mainwindow.
    In particular I only want to append text to the textEdit on the mainwindow and modify the progress bar value, during the QThread calculation.

    I tried the heritage method but it doesn't works.

    Ex. of my app:
    Qt Code:
    1. #ifndef HRSMAINWINDOW_H
    2. #define HRSMAINWINDOW_H
    3.  
    4. #include <QtGui/QMainWindow>
    5.  
    6. namespace Ui
    7. {
    8. class HRSMainWindowForm;
    9. }
    10.  
    11. class HRSMainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. HRSMainWindow(QWidget *parent = 0);
    17. ~HRSMainWindow();
    18. void APT(QString a){ //append text to the textEdit
    19. ui->textEdit->append(a);
    20. }
    21.  
    22. private:
    23. Ui::HRSMainWindowForm *ui;
    24. };
    25.  
    26. #endif // HRSMAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef HRSTHREAD_H
    2. #define HRSTHREAD_H
    3.  
    4. #include <QThread>
    5. #include "hrsmainwindow.h"
    6.  
    7. class HRSThread : public QThread, public HRSMainWindow
    8. {
    9. public:
    10. void run(){
    11. APT("Hello World"); // don`t work
    12. }
    13. };
    14.  
    15. #endif // HRSTHREAD_H
    To copy to clipboard, switch view to plain text mode 

    Thank you very much

  2. #2
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Control MainWindow from QThread

    sorry buddy, but u cant perform any GUI operation through a QThread in QT...

  3. #3
    Join Date
    Feb 2009
    Location
    ŁÃ³dź, Poland
    Posts
    20
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Control MainWindow from QThread

    You still can emit signals from a running thread, that, connected to slots in MainWindow would modify the textEdit. You can set the thread class to be a member of MainWindow if you like, but the best solution would be having the thread and MainWindow as two separate classes. The connection can be made in main.cpp.

  4. #4
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Control MainWindow from QThread

    read thread support in Qt and blocking fortune client example .. its easier to use threads eventhough we cant use QWidget .. QWidgets are not reentrant but we can connect between the main thread and worker thread

  5. #5
    Join Date
    Sep 2008
    Posts
    60
    Thanks
    20
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Control MainWindow from QThread

    Hi everybody,

    Thank you very much, know I understand what I have to do, the Blocking Client example is absolutely perfect!!

Similar Threads

  1. Replies: 1
    Last Post: 11th September 2007, 13:34
  2. Replies: 6
    Last Post: 3rd February 2006, 09:48

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.