Results 1 to 6 of 6

Thread: Multithreading application and GUI

  1. #1
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Multithreading application and GUI

    Hi! I'm trying to write an application where I show thumbnails of files in a directory. I would like, of course, the GUI to be quite responsive while thumbnails are created. Therefore, I wrote a thread which creates thumbnails and adds them to a subclass I made of QGraphicsView. It seems it's behaving quite good, but not entirely as I thought it would.

    The strange thing is that, when I run my application, instead of starting to show, one by one, the generated thumbnails, it waits a little bit, and then shows, all together, many thumbnails. After that, again it waits quite some time, and again shows many thumbnails... etc.... I mean that it seems the GUI is not showing thumbnails as soon as the thread gives them, but at certain points, after some are collected.

    Is there anyone who can try to guess why? The generating thread has the LowestPriority, so I suppose, when I tell the view to show the images, the GUI should do that immediately having higher priority...

    My suspect is that, this line in my generating thread:
    Qt Code:
    1. graphicsView->addThumbnail(thumbnail);
    To copy to clipboard, switch view to plain text mode 
    which adds my thumbnail to the QGraphicsView using the addItem(...) method, is run within my generating thread, and not the main thread (which manages the GUI). Is this correct? But in case this was true, shouldn't it wait until the thumbnail was shown before generating the following thumbnail? Still doesn't explain the result. Any idea?
    Thanks for any help!

  2. #2
    Join Date
    Oct 2008
    Posts
    37
    Thanked 2 Times in 2 Posts

    Default Re: Multithreading application and GUI

    I've written several apps that do something similar - in Qt and before in Wx. I take an approach that is similar to the one that you described with a few differences. In the thread that I'm resizing the image to make it fit for the the QGraphicsView to display - I send a signal everytime I'm doing with an image.

    IIRC, I generally just package up the data into a pointer and just send it. In my main thread, I just read the data back out and do the appropriate thing so that QGraphicsView can display it. After that I ask QGraphicsView to refresh itself and maybe the child I just added.

    If you don't ask QGraphicsView to refresh, it might just wait until the next time it needs to fresh and does it then. Which might explain why you're seeing them in batches. If you already are doing this, then something else might be going on that's causing it not to update when it should.

  3. #3
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Multithreading application and GUI

    I'm suspecting that the way I'm behaving is not correct cause that way I would do GUI-related operations with a non-GUI thread... Anyway, I use, after all addItem a setScene to the itemsBoundingRect to fit the scene to the content. I suppose this should refresh the content immediately after adding each item... Am I wrong?
    Thanks for your advices!

  4. #4
    Join Date
    Oct 2008
    Posts
    37
    Thanked 2 Times in 2 Posts

    Default Re: Multithreading application and GUI

    If you emit a signal from your non-GUI thread and process it in your main thread, where you would do the refresh, I think it should be okay.

    From my understanding, yes you should refresh after each item is added if you want to see it immediately. That's how I handle it in my image browser an it seems to work alright.

  5. #5
    Join Date
    Dec 2009
    Posts
    29
    Thanks
    2
    Thanked 3 Times in 3 Posts

    Default Re: Multithreading application and GUI

    Do you really have a GUI-thread? If you didnt moveToThread(this); in your constructor and you instantiated the QThread in the mainthread then it is still the main thread that handles your process.
    Last edited by yodasoda; 2nd March 2010 at 08:47. Reason: precision

  6. #6
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Multithreading application and GUI

    I solved using a connection and making a correct repaint of the view. Thanks!

Similar Threads

  1. Replies: 2
    Last Post: 11th February 2010, 10:35
  2. regarding multithreading
    By mohanakrishnan in forum Qt Programming
    Replies: 19
    Last Post: 9th December 2009, 09:21
  3. multithreading
    By mickey in forum General Programming
    Replies: 2
    Last Post: 5th June 2008, 23:01
  4. D-Bus in Multithreading environment
    By mdecandia in forum Qt Programming
    Replies: 0
    Last Post: 15th March 2007, 17:36
  5. MultiThreading in Qt
    By manivannan_1984 in forum Qt Programming
    Replies: 7
    Last Post: 7th November 2006, 20:26

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.