Results 1 to 6 of 6

Thread: Loading (e. g. a qtreeview) model data in a different thread

  1. #1
    Join Date
    Dec 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Loading (e. g. a qtreeview) model data in a different thread

    Hi,

    I stumbled over this thread: http://www.qtcentre.org/threads/3253...t-loading-data and it seems this problem is similar to mine. The solution proposed in this related thread does not help me much (because I don't know how it was implemented). But maybe I don't get some basic concept of Qt or graphical frameworks in general. How is it possible to load data in a separate thread so that the GUI does not block?

    On one hand data that is loaded in a dedicated thread cannot be used in the main GUI-thread (Error: "QObject: Cannot create children for a parent that is in a different thread."). On the other hand I don't know how the main GUI-thread knows when (e. g. by initiating the widget to reload the model itself) and how (e. g. by setModel() with valid data from the same or a different thread) to use the new model.

    Actually I just want to load vast amounts of data in a dedicated thread to display a "loading" picture or something similar until the data has become available so that the user knows what's going on.

    Thanks for your help and best regards,
    Rainer

  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: Loading (e. g. a qtreeview) model data in a different thread

    Quote Originally Posted by dubbaluga View Post
    On one hand data that is loaded in a dedicated thread cannot be used in the main GUI-thread (Error: "QObject: Cannot create children for a parent that is in a different thread.").
    Not every "data" is a QObject. The model itself belongs to the main thread but it doesn't mean data for it can't be prepared in another thread.

    Actually I just want to load vast amounts of data in a dedicated thread to display a "loading" picture or something similar until the data has become available so that the user knows what's going on.
    So where does the thread fit in?
    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
    Join Date
    Dec 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Loading (e. g. a qtreeview) model data in a different thread

    Hi,

    thanks for your quick reply!

    Quote Originally Posted by wysota View Post
    Not every "data" is a QObject. The model itself belongs to the main thread but it doesn't mean data for it can't be prepared in another thread. So where does the thread fit in?
    I'm not completely sure, but I would pass a reference of the QAbstractItemModel from the "main thread" to the "data preparing" thread. This thread (the latter) in turn adds QModelindex objects (as they contain the internal void* pointer) by value to e. g. the setData()-method of that model.

    Could it work that way?

    Best regards,
    Rainer

  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: Loading (e. g. a qtreeview) model data in a different thread

    Quote Originally Posted by dubbaluga View Post
    I'm not completely sure, but I would pass a reference of the QAbstractItemModel from the "main thread" to the "data preparing" thread. This thread (the latter) in turn adds QModelindex objects (as they contain the internal void* pointer) by value to e. g. the setData()-method of that model.

    Could it work that way?
    No, that's not a good idea. You should prepare the data in external thread and signal the model object that it can consume what's prepared for it so that the modification to the model happens in the main thread.
    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.


  5. #5
    Join Date
    Dec 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Post Re: Loading (e. g. a qtreeview) model data in a different thread

    Quote Originally Posted by wysota View Post
    No, that's not a good idea. You should prepare the data in external thread and signal the model object that it can consume what's prepared for it so that the modification to the model happens in the main thread.
    So, it's working for me now. I'm passing the underlying data structure of my QAbstractItemModel object, which is an instance of a composite pattern class to the worker thread so that it can add additional elements to it.
    Before doing so, the beginResetModel() method is invoked. After adding the elements, the endResetModel() method of my QAbstractItemModel instance is invoked.

    What helped me a lot was actually the point, that GUI parts have to stay in the main thread (GUI thread). In case I want to display some "work in progress" message, I'm using Signals/Slots between my worker thread and main thread, as mentioned here.

    As I'm actually programming with PySide, further relevant links can be found here:


    Thanks for your help! Best regards, Rainer

  6. #6
    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: Loading (e. g. a qtreeview) model data in a different thread

    I'm not convinced what you are doing is good. You are manipulating the same data structure from two threads without proper synchronization. This is prone to race conditions and can crash your application or make it behave weird.
    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. Loading data in a thread
    By Cruz in forum Qt Programming
    Replies: 23
    Last Post: 28th October 2010, 17:18
  2. Replies: 1
    Last Post: 19th July 2010, 11:43
  3. Flat data source for QTreeView/Model
    By mawt in forum Qt Programming
    Replies: 1
    Last Post: 18th October 2009, 16:20
  4. Modify model data in QTreeView
    By YuriyRusinov in forum Qt Programming
    Replies: 6
    Last Post: 26th October 2006, 17:28
  5. [QT4 & XP] retrieve data from QTreeView's model
    By incapacitant in forum Newbie
    Replies: 1
    Last Post: 2nd March 2006, 13:02

Tags for this Thread

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.