Results 1 to 3 of 3

Thread: Is it possible to share a model between multiple views but display different records?

  1. #1
    Join Date
    Feb 2010
    Posts
    17
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Is it possible to share a model between multiple views but display different records?

    I feel like this place is my lifeline the past couple of weeks!

    Does anybody know if it's possible to have a single model shared by multiple views, but, showing different subsets of data?

    My scenario:
    I have a model that represents a database table QSqlTableModel or QSqlRelationalTableModel and I have multiple views on a single screen. I wish to display, for example, all records created before date X in one view and all records created after date X in another view.

    The only examples I've found so far that allow the sharing of models between multiple views seem to keep the same records displayed in each view, rather than displaying different subsets of data.

    Is this possible, or will I need to instantiate multiple models? I'm trying to fiddle with QSortFilterProxyModel now, but I don't think it's entirely suitable for what I want here.

    Thanks!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Is it possible to share a model between multiple views but display different reco

    Yes. QSortFilterProxyModel (or another QAbstractProxyModel derivative) is designed precisely for this. Ingredients:
    • One base QSqlTableModel instance. Data is stored once in memory.
    • One QSortFilterProxyModel derivative for each view. Override filterAcceptsRow() as required to select the rows you want, and lessThan() if you want to sort them in some special way. Set the sourceModel() to your base model. You could get away with the one proxy class with an "Invert filter" option: there'd still be two instances.
    • Two views. Set the model to an instance of the relevant QSortFilterProxyModel derivative.

    Links to the relevant tutorials are in the QSortFilterProxyModel page.

  3. The following user says thank you to ChrisW67 for this useful post:

    stevebakh (25th March 2010)

  4. #3
    Join Date
    Feb 2010
    Posts
    17
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Is it possible to share a model between multiple views but display different reco

    Thanks a lot for the tip. I'm in the process of implementing it now and it seems this is what I needed afterall!

Similar Threads

  1. Scene vs. multiple views
    By lni in forum Qt Programming
    Replies: 16
    Last Post: 19th July 2022, 01:47
  2. how to disable views from a model
    By zeeeend in forum Qt Programming
    Replies: 3
    Last Post: 9th September 2008, 21:14
  3. Question about a model with multiple views
    By awhite1159 in forum Qt Programming
    Replies: 3
    Last Post: 26th June 2008, 00:27
  4. Replies: 1
    Last Post: 16th January 2008, 11:48
  5. 2 Views 1 Model
    By PrimeCP in forum Qt Programming
    Replies: 3
    Last Post: 2nd October 2007, 01:40

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.