Results 1 to 13 of 13

Thread: change QWidget from QThread classes

  1. #1
    Join Date
    Feb 2010
    Posts
    28
    Qt products
    Qt4
    Platforms
    Windows

    Default change QWidget from QThread classes

    Hi everybody!

    I have a QMainWindow application with some QThread classes. I'm changing the GUI widgets from QThread objects and partially it works properly.

    if I have only one instance of my custom class (it is Qthread and this one makes instances of other QThread classes with QTcpServer/socket/ QUdpSocket) and I change the GUI widgets from these it works properly. For a second instance it works as well but for a third instance it works bad. What I saw is, the widgets stop updating and if a minimized the GUI window and after that I maximize the window again the widgets are changing and after a while it crases...

    So I read that you can only change GUI widgets from the main thread but I'm doing it from other thread and it seems to be not so bad...

    what can you recommend me? do you know why I have these crashes to update the gui? do I have one global mutex for all classes in order to update the gui?

    Thanks in advance

  2. #2
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: change QWidget from QThread classes

    You cannot update gui directly from QThread's functions. You should emit signals in threading functions, that should be connected to slots in a gui class.
    for example:
    Qt Code:
    1. class MyThread : public QThread
    2. {
    3. Q_OBJECT
    4. ...
    5. signals:
    6. void updateLabel( QString str );
    7. };
    8.  
    9. void MyThread::run()
    10. {
    11. ...
    12. emit updateLabel( "the text" );
    13. }
    14.  
    15. class MyGuiClass : public QMainWindow
    16. {
    17. Q_OBJECT
    18. ...
    19. public slots:
    20. void onUpdateLabel( QString str );
    21. };
    22.  
    23. MyGuiClass::MyGuiClass( )
    24. {
    25. ...
    26. m_thread = new MyThread();
    27. connect( m_thread, SIGNAL( updateLabel( QString ) ), SLOT( onUpdateLabel( QString ) ) );
    28. }
    29.  
    30. void MyGuiClass::onUpdateLabel( QString str )
    31. {
    32. ui.label->setText( str );
    33. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2010
    Posts
    28
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: change QWidget from QThread classes

    Thanks borisbn, I'll try you proposal.

    Do I need something like a buffer to store the data in case of quick updates?

    Thanks!

  4. #4
    Join Date
    Feb 2010
    Posts
    28
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: change QWidget from QThread classes

    Hi again,

    I have some problems to transmit the signal between thread. I used your example to programme my signals/slots but the problem is that I can't get the signals.

    The structure that I have is:

    MyGUIClass:
    instance of QTthread1

  5. #5
    Join Date
    Feb 2010
    Posts
    28
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: change QWidget from QThread classes

    sorry! I struggled with my keyboard!

    so, I have this structure in my programm:

    MyGUIClass:
    instance of QThread1

    QThread1:
    instance of QThread2 and QThread3

    I need to send signals from Qthread2 to Qthread1 and the from Qthread1 to MyGUIClass (first GUI update)
    and then I need a signal from QThread1 to MyGUIClass ( second UI update)

    So I did it, I'm testing now and I don's get the signal from QThread2 to 1, so I can't see anything in the GUI

    I tried to use the flag Qt:irectConnection in the connect but It doesnn't works

    do you have any ideas?

    thanks!

  6. #6
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: change QWidget from QThread classes

    1. (about buffer between thread and gui) signal's parameter are buffers (e.g. copies). But if you need a huge number of data to transmit from thread to gui, you can alloc them by new and pass a pointer to gui. Then gui should proceed data, and delete this pointer
    2. (about transmitting signal from "child of child" to parent) you can create a signal in thread2 and thread3 classes and connect them to signal of a thread1 object
    Qt Code:
    1. class Thread2 : public QThread
    2. {
    3. ...
    4. signals:
    5. void thread2_signal(QString str);
    6. };
    7.  
    8. Thread1::Thread1()
    9. {
    10. thread2 = new Thread2();
    11. connect( thread2, SIGNAL(thread2_signal(QString)), SIGNAL(thread1_signal(QString));
    12. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Feb 2010
    Posts
    28
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: change QWidget from QThread classes

    thanks for your help borisbn,

    I've tried to transmit a signal from "child of child to parent" and I didn't manage to catch the signal.

    I did this:

    thread 1 --> parent
    thread 2 --> child1
    thread 3 --> child2 (child of child1)

    in thread2 I connect thread3_signal to thread2_signal :

    Qt Code:
    1. connect( thread3, SIGNAL(thread3_signal(QString)), SIGNAL(thread2_signal(QString));
    To copy to clipboard, switch view to plain text mode 

    in thread1 I connected the signal from thread2 to the slot in thread 1, that will be update the GUI:

    Qt Code:
    1. connect( thread2, SIGNAL(thread2_signal(QString)), SLOT(thread1_slotl(QString));
    To copy to clipboard, switch view to plain text mode 

    this way I don't get the signal in thread1

    do you have some ideas?

    thanks in advance!!

  8. #8
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: change QWidget from QThread classes

    It's not easy to understand a problem by pieces of code. If you'll archive your project and attach it to the next post, I'll try to assist.
    P.S. Try to create several signals/slots in one class e.g. main gui class. Connect them in a way you want and see, what will happen (or not happen )

  9. #9
    Join Date
    Feb 2010
    Posts
    28
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: change QWidget from QThread classes

    ok, this is my code:

    main thread:
    Qt Code:
    1. // function for button start
    2.  
    3. void mainThread::clickedStart(){
    4. obj2 = new seconThread(params);
    5. connect( obj2 , SIGNAL( update1( int, int ) ), SLOT( onUpdate1( int, int ) ) );
    6. connect( obj2 , SIGNAL( update2( int, int ) ), SLOT( onUpdate2( int, int ) ) );
    7. obj2 ->start();
    8. }
    9.  
    10. // slot update1
    11. void mainThread::onUpdate1(int a, int b){
    12.  
    13. ui.showtrace1->setText(QString("%1").arg(a));
    14. ui.showtrace2->setText(QString("%1").arg(b));
    15.  
    16. }
    17.  
    18. // slot update2
    19. void mainThread::onUpdate2(int a, int b){
    20.  
    21. ui.showtrace3>setText(QString("%1").arg(a));
    22. ui.showtrace4->setText(QString("%1").arg(b));
    23. }
    To copy to clipboard, switch view to plain text mode 

    second thread:
    Qt Code:
    1. void seconThread::run(){
    2.  
    3. obj3 = new thirdThread(params);
    4. connect( obj3 , SIGNAL( update3(int, int) ), SIGNAL(update1 (int, int) ) );
    5. connect( obj3 , SIGNAL( update4(int, int) ), SIGNAL( update2(int, int) ) );
    6. obj3 ->start();
    7. }
    To copy to clipboard, switch view to plain text mode 

    third thread:

    Qt Code:
    1. void thirdThread::run(){
    2.  
    3. while(control){
    4. // some prrcess
    5. emit update3( a, b);
    6. emit update4( c, d);
    7.  
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    The point is, I can't get the signals update1 and update2, I've tried this way (connect signal to signal in the second thread)and to send a signal from thread3 to thread2 in a slot and then to send a signal from this to the main thread but it didn't work.

    P.S. I tried to make the connection queued too, but no luck!!

    Thanks in advance!!

  10. #10
    Join Date
    Feb 2010
    Posts
    28
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: change QWidget from QThread classes

    I have it!!

    my problem was with one slot function. It takes two params and I gave only one!

    I noticed it when I was posting the last thread,

    sorry for the trouble and thanks to Borisbn, you are right, it's better to post the code

    Thank you very much!

  11. #11
    Join Date
    Feb 2010
    Posts
    28
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: change QWidget from QThread classes

    so, now another problem, it works finel if I only have one instance of secondThread in the main thread but if I have two instances, I don't get the signals anymore.

    what happens?

  12. #12
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: change QWidget from QThread classes

    Take a look in a output window of your debugger. Qt writes thereany problem occured while connecting signals/slots and emitting signals. In your case Qt said you about wrong number of parameters (change back and make sure)

  13. #13
    Join Date
    Feb 2010
    Posts
    28
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: change QWidget from QThread classes

    thanks borisbn, it helped me!

Similar Threads

  1. Replies: 1
    Last Post: 12th April 2010, 12:55
  2. Replies: 7
    Last Post: 15th January 2010, 20:45
  3. change QGraphicsView* in QThread
    By deca5423 in forum Qt Programming
    Replies: 4
    Last Post: 30th June 2009, 01:12
  4. how to change shape of Qwidget??
    By anupamgee in forum Qt Programming
    Replies: 4
    Last Post: 29th June 2009, 09:54
  5. Replies: 1
    Last Post: 2nd May 2006, 21:11

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.