Results 1 to 4 of 4

Thread: ListView not being updated at runtime to follow the model

  1. #1
    Join Date
    Jun 2012
    Posts
    58
    Thanks
    13
    Qt products
    Qt4

    Default ListView not being updated at runtime to follow the model

    Hi,
    In our new project we have decided to use qml for the 1st time. Having spent sometime learning qt quick 1.0 in Qt 5.0, we suddenly have Qt 5.1 coming up which focuses on qml on desktops - what we need. Since 5.1 is still beta it is buggy (for QQuickView and in general QWindow maximize is not available - https://bugreports.qt-project.org/browse/QTBUG-31258). I want to know if the following issue is a bug or is the result of wrong coding on my part:
    I make a class derived from QAbstractListModel and re-implement the necessary functions. I create an object of it, fill in some initial data into the model (all beginInsertRows etc done) and then pass it (the object) to qml via setContextProperty. For qt 5.1 with quick 1.0 I use QDeclarativeView. For qt 5.1 with quick 2.0 I use QQuickView. Once qml is 'shown' by show() command, I spawn a std::thread in which I pass the reference of the Model object. The thread keeps adding data to the model via direct call to Model's addData(..) function (which has beginInsertRows etc) using the passed reference every 'x' milliseconds. Meanwhile in the main thread QApplication's exec() would have started.

    Problem: Using qt 5.1 quick 1.0 (QDeclarativeView) there is no problem - the ListView (UI) updates fine and keeps growing showing it is indeed following the model. Using quick 2.0 (QQuickView) breaks this behaviour. There is no update of the UI other than the initial data set into the model (described before). If I resize or imitate a flick with the mouse then suddenly all data contained upto that point is shown but again things halt there. I get the following listed in the Application Output pane in QtCreator:
    QObject::connect: Cannot queue arguments of type 'QQmlChangeSet'
    (Make sure 'QQmlChangeSet' is registered using qRegisterMetaType().)

    I don't know what QQmlChangeSet is. However if I use QThread derived class and re-implement run() to do the same, but instead of passing the refernece of Model's object I emit signal which is caught by the Model's addData(..) function which I've now turned into a slot, then everything works fine again and the ListView UI grows in number of rows. Is this expected? If so then why (ie., what is wrong with the former approach)?

  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: ListView not being updated at runtime to follow the model

    Your code is invalid. You cannot modify the model from within a worker thread. If you really have to use a thread, then emit a signal from it and connect it to a slot in the model that will perform the addition from 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.


  3. #3
    Join Date
    Jun 2012
    Posts
    58
    Thanks
    13
    Qt products
    Qt4

    Default Re: ListView not being updated at runtime to follow the model

    Finally you come to my rescue

    Your code is invalid.
    Why does it work with Qt Quick 1.0?

    You cannot modify the model from within a worker thread. If you really have to use a thread, then emit a signal from it and connect it to a slot in the model that will perform the addition from the main thread.
    Yes i guessed that because as stated, when i used the QThread way (similar to what you suggest), everything worked fine. Why can't model be updated in a worker thread? It's not a UI component. Could you please also give me a link to the documentation where this is stated? It's not in QAbstractItemModel doc.

  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: ListView not being updated at runtime to follow the model

    Quote Originally Posted by ustulation View Post
    Why does it work with Qt Quick 1.0?
    No idea, it shouldn't. But QtQuick 2 and QtQuick 1 have a totally different implementation, maybe that's the reason.

    Yes i guessed that because as stated, when i used the QThread way (similar to what you suggest), everything worked fine.
    It doesn't matter if you use QThread or std::thread. The issue remains the same.

    Why can't model be updated in a worker thread? It's not a UI component.
    The basic idea is that the same resource cannot be accessed by more than one thread at the same time without proper synchronisation. The real problem in this particular case is a bit different (cross-thread signals and slots) but the original issue remains the same. You simply shouldn't ever try to do this.

    Could you please also give me a link to the documentation where this is stated? It's not in QAbstractItemModel doc.
    Go to QObject docs and search for "thread affinity".
    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. The following user says thank you to wysota for this useful post:

    ustulation (15th June 2013)

Similar Threads

  1. Model Items do not updated
    By rimie23 in forum Qt Programming
    Replies: 1
    Last Post: 8th June 2012, 21:26
  2. Custom ListView. Using the model / view framework.
    By plopes21 in forum Qt Programming
    Replies: 19
    Last Post: 8th May 2012, 08:43
  3. ListView model binding to QAbstractListModel
    By michalk in forum Qt Quick
    Replies: 1
    Last Post: 16th July 2011, 09:21
  4. relaional model not updated
    By vincentb in forum Qt Programming
    Replies: 0
    Last Post: 8th December 2010, 14:11
  5. Replies: 1
    Last Post: 25th September 2010, 08:20

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.