Results 1 to 11 of 11

Thread: Update widgets independently and simultaneously

  1. #1
    Join Date
    Jul 2013
    Posts
    16
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Update widgets independently and simultaneously

    Using 5.0.2 on Win 7

    I have 2 "ImageLoop" classes [which extend QWidget] where each take up half the screen, split vertically. Each contains a QLabel for displaying a list of jpg files. So, inside a for loop, I give each widget their list of images, and emit a "listfull" signal which I have connected to a slot - "playList" - in each of the two widgets. Unfortunately, it appears that only the first widget's signal ever gets emitted as only the first widget is ever updated.

    I am new to Qt programming and maybe I am misunderstanding the slot/signal system. I thought the pseudocode below would, for each instance, fill the list, emit the signal, and each instance would go off on their merry way -- basically each widget simultaneously and independently showing images. So, question is what am I missing? Or am I going to have to spawn each of these in their own thread?

    Thanks!

    Pseudocode
    Qt Code:
    1. for(int i=0; i<2; i++){
    2. Create ImageLoop instance
    3. connect(instance, SIGNAL(listfull()), instance, SLOT(playList()));
    4. instance->FillList(arrayOfImageFileNames);
    5. }
    6.  
    7. //inside of ImageLoop class
    8. void FillList(arrayOfImageFileNames) {
    9. //adds all files to an internal list
    10. //when finished
    11. emit listfull();
    12. }
    13.  
    14. //inside of ImageLoop class
    15. void playList() {
    16. //code to loop through each image and show it
    17. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Update widgets independently and simultaneously

    I am new to Qt programming and maybe I am misunderstanding the slot/signal system. I thought the pseudocode below would, for each instance, fill the list, emit the signal, and each instance would go off on their merry way-- basically each widget simultaneously and independently showing images
    NO, each widget (QLable in your case) will be painted (upated) one after another.

    Your code does not show any problem, it should word as you ecpected but in a a sequence (one after another) not is parallel. Check if there are any messages in Applicatio output, also make user Q_OBJECT macro is defined and signal and slots are declared in proper sections of class definiction.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Jul 2013
    Posts
    16
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Update widgets independently and simultaneously

    Thank you sir. I receive no errors or other indication in my application output and I have the signals and slots properly set up in my class definition. One big indicator is that it works for the first widget. So, what would I need to get them to run in parallel?

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Update widgets independently and simultaneously

    One possible problem could be (as the actual code is not shown) that the your playlist() function has a non returning loop.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    Join Date
    Jul 2013
    Posts
    16
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Update widgets independently and simultaneously

    This is true, it does not. Each widget just needs to sit there and loop over the list of images over and over.

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Update widgets independently and simultaneously

    You cannot do so.

    If you need to update the both the widgets often then use a timer to trigger the signals to update, and DO NOT use non retunring loops, it will cause the GUI (or parts of GUI) to not respond (freeze/Hang)
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  7. #7
    Join Date
    Jul 2013
    Posts
    16
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Update widgets independently and simultaneously

    That is just it, each of those methods use a qtimer to change out the images. The problem is that it is only showing the first imageloop instance. The other half of the screen is black and never shows.

    Could I put each instance in its own QThread? Would that make it work? The gui being responsive is not a huge deal as the whole point is to start the program and let the two sections of the screen each run through their list of images until the program is shutdown.

  8. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Update widgets independently and simultaneously

    Could I put each instance in its own QThread? Would that make it work?
    No, GUI components cannot be directly updaated from QThread.

    Only option you have is to use timer (QTimer / QObject::startTimer()), which is lot sipler than QThread
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  9. #9
    Join Date
    Jul 2013
    Posts
    16
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Update widgets independently and simultaneously

    So, would change code like this?

    Qt Code:
    1. for(int i=0; i<2; i++){
    2. Create ImageLoop instance
    3. instance->FillList(arrayOfImageFileNames);
    4. }
    5.  
    6. //inside of ImageLoop class
    7. void FillList(arrayOfImageFileNames) {
    8. //adds all files to an internal list
    9. //when finished
    10. QTimer::singleShot(0, this, SLOT(playList()));
    11. }
    12.  
    13. //inside of ImageLoop class
    14. void playList() {
    15. //code to loop through each image and show it
    16. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Update widgets independently and simultaneously

    1. You have to use multi-shot timer
    2. In playList() slot update each image and return, so that on next timeout the slot is called again
    Last edited by Santosh Reddy; 12th July 2013 at 21:22.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  11. #11
    Join Date
    Jul 2013
    Posts
    16
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Update widgets independently and simultaneously

    Gotcha! thanks. I have it now. I appreciate the help.

Similar Threads

  1. Replies: 8
    Last Post: 5th May 2021, 16:41
  2. Replies: 10
    Last Post: 30th January 2011, 14:32
  3. Replies: 2
    Last Post: 23rd July 2010, 03:41
  4. how to force widgets update
    By Onanymous in forum Newbie
    Replies: 5
    Last Post: 14th June 2010, 17:37
  5. Widgets visual update / synchronization
    By Caius Aérobus in forum Qt Programming
    Replies: 4
    Last Post: 27th November 2007, 12:52

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.