Results 1 to 4 of 4

Thread: Do I need update model in QT UI thread?

  1. #1

    Unhappy Do I need update model in QT UI thread?

    Hi guys,

    I was told that if you want to update the model of a widget, you need do it in QT thread, means you need emit a signal and update model in slot. Is that true?

    The problem I faced is I change a tree view's model in a thread, but during I updating the model, QT invoke some draw process.
    Because the model is deleted, some value is not available, some index is incorrect, then GUI crashed.

    Then I found some function "beginResetModel", "endResetModel". I put them when I update the model.
    Some times the QT still call draw process and crash the GUI.

    Any comments?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Do I need update model in QT UI thread?

    If two threads access the same data, their access needs to be synchronized with mutexes or similar mechanisms.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3

    Default Re: Do I need update model in QT UI thread?

    Thanks, but nothing to do with my question.

    My question is what is the standard way when you want to destroy all model and reset the widget?
    You mean I need override all draw method and add mutex on all of them?

    It's always happen, very common question.
    When you design a real product you will have some data object and show these data on GUI. Every time these data can be totally different, so you have to delete them all and create the tree again. QT should have some mechanism to avoid painting during model is unavailable, right?
    Last edited by ysilent; 23rd March 2012 at 02:01.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Do I need update model in QT UI thread?

    Quote Originally Posted by ysilent View Post
    Thanks, but nothing to do with my question.
    On the contrary, everything to do with your question.

    My question is what is the standard way when you want to destroy all model and reset the widget?
    You mean I need override all draw method and add mutex on all of them?
    No, I mean that if you want to change the model from within a worker thread, you need to synchronize this thread with the main thread so that you're not modifying internal structure of the model while the main thread reads data from the model.
    Thus you should wrap all access to those internal structures in mutexes.

    QT should have some mechanism to avoid painting during model is unavailable, right?
    The model is always available. If you want, you can create the new structure outside the model and then only replace the old structure with the new one and reset the model. The latter has to be done from the main thread, the former (rebuilding the model structure) can be done from a worker thread. Both threads need to be synchronized when the internal data is to be replaced.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. use Thread update many GUI
    By phuong_90 in forum Qt Programming
    Replies: 6
    Last Post: 20th November 2011, 05:23
  2. Using QWidget::update() from non-GUI thread
    By some_birdie in forum Qt Programming
    Replies: 3
    Last Post: 21st June 2011, 09:11
  3. Update GUI in a thread
    By olivier1978 in forum Qt Programming
    Replies: 3
    Last Post: 8th January 2011, 21:33
  4. Update GUI from another thread
    By Anne in forum Qt Programming
    Replies: 9
    Last Post: 14th July 2010, 15:08
  5. update a wigdet in a thread is no allow?
    By lanmanck in forum Qt Programming
    Replies: 8
    Last Post: 25th October 2009, 03:05

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.