Results 1 to 6 of 6

Thread: C++ class with multiple QAbstractListModel?

  1. #1
    Join Date
    Sep 2016
    Posts
    6
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    3

    Default C++ class with multiple QAbstractListModel?

    I have several C++ models implemented using QAbstractListModel and showing these in QML using Repeaters.
    Every of the C++ QAbstractListModel derived classes is registered independently and instantiated in QML.

    What I would like to have is one C++ class with multiple models that I show in QML. That is since I would like to implement in C++ cross-model logic that needs to have access to all the models data and manipulate them accordingly.
    I currently do not have this connection in my design.

    Appreciate if someone could point me into the right direction.

    Thanks,
    Ben

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,349
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: C++ class with multiple QAbstractListModel?

    It only makes sense if the models are each a different subset of the same data (eg. each model is the result of searching using a different key) or if the models are different data linked by a common key (eg. user name and address info, the items a user has bought, the set of individual purchases by that user. "user" is the common key).

    In the first type, you decide how you concatenate the models - if one model has 20 rows and the next has 34, then your "pseudo-model" reports it has 54 rows and returns information from rows 0 - 19 from the first model, and from the second model for rows 20 - 53.

    In the second type, it is more complex. Your pseudo-model will have as many columns as all models combined, and will have columns that repeat for several rows (eg. name and address will repeat for each item purchased entry for that user).

    How you do this will depends on the specific details.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Sep 2016
    Posts
    6
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    3

    Default Re: C++ class with multiple QAbstractListModel?

    Thanks for your reply d_stranz.

    To make it more specific -
    Let's assume the application should give the user the option to add rectangles, circles and triangles from the QML GUI.
    Currently I have 3 QAbstractListModel classes, one for rectangles, one for circles and one for triangles. Each of the types is displayed by a Repeater module in QML running over the QAbstractListModel with the correct delegate.

    I would like to have another C++ "master class" that can have access to all of the 3 lists to manipulate the data.

    Thanks,
    Ben

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

    Default Re: C++ class with multiple QAbstractListModel?

    Quote Originally Posted by benberli View Post
    I would like to have another C++ "master class" that can have access to all of the 3 lists to manipulate the data.
    So why don't you do exactly that? Have an object which keeps pointers to all the models and manipulates them according to your wishes.
    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
    Sep 2016
    Posts
    6
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    3

    Default Re: C++ class with multiple QAbstractListModel?

    Quote Originally Posted by wysota View Post
    So why don't you do exactly that? Have an object which keeps pointers to all the models and manipulates them according to your wishes.
    Well, just not sure how to technically achieve it.
    I have currently 3 classes deriving from QAbstractModel. Their instantiation is done in QML separately.
    Not sure how to create a 4th class and pass the pointers to it .. I mean, how do I get an object pointer from QML and pass it to a C++ class?
    Another option is maybe to create a class where the QAbstractModels are children of this class. But then I don't know how in QML to access the children and pass as models to different repeaters.

    Sorry, I am sure this is basics..

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: C++ class with multiple QAbstractListModel?

    You create a class that has three properties, one for each model.
    The type of the property can be the pointer type of the specific model class or just "QAbstractItemModel*"

    If the class creates the models in C++, these properties can be read-only and CONSTANT, if you want to create the model instances in QML then they are read-write with a NOTIFY signal.

    Given your description of your data (geometric shapes), you could potentially also do a model that contains all items and has an additional role for the type of shape, using that to select delegates for a single repeater.

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    benberli (9th September 2016)

Similar Threads

  1. Replies: 8
    Last Post: 11th March 2016, 09:56
  2. multiple QListViews on one QAbstractListModel
    By tesmai4 in forum Qt Programming
    Replies: 1
    Last Post: 5th November 2013, 18:51
  3. Multiple threads in a single class
    By Matthias81 in forum Qt Programming
    Replies: 2
    Last Post: 22nd August 2013, 07:06
  4. How to use a class multiple times?
    By xtlc in forum Newbie
    Replies: 5
    Last Post: 8th August 2013, 15:48
  5. Replies: 11
    Last Post: 24th July 2012, 12:39

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
  •  
Qt is a trademark of The Qt Company.