Results 1 to 14 of 14

Thread: View using multiple models

  1. #1
    Join Date
    Dec 2009
    Posts
    47
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default View using multiple models

    I'm trying to build a plot (view) of two or more sets of data, and therefore have two or more models into that data. The resultant view needs to be synced with these multiple models. Is there a reasonable way to merge models that has already been used?

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

    Default Re: View using multiple models

    A proxy model, that's for sure... But how do these models relate to each other? You want to merge them horizontally or vertically?
    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 2009
    Posts
    47
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: View using multiple models

    Horizontally. I have a set of data points (typically 10, to 1oooo points) and fitted analytical curve(s) that need to be overlaid for comparison. I have set up the obvious: a PointsModel to handle the data which populates a QTableView. Now I need to build a plot in a separate view that plots the histogram of the data (using a (vertical) proxy model to calculate the bins) and overlays one or more possible fits. I can represent a fit with an appropriate model, but I don't know how to merge it (them) with the proxy model, especially because the multiple fit models will be signaled from a QListWidget selection(s). Thanks for any help.

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

    Default Re: View using multiple models

    Implement a proxy that will work on two models - the supported sourceModel() and another model. Reimplement index() and data() (and possibly other methods) to take both models into consideration instead of just the sourceModel().
    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 2009
    Posts
    47
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: View using multiple models

    I have been working in that direction. However, data() takes Qt:isplayRole to return the appropriate data. If I implement multiple responses, I need to use Qt::UserRoles(s) which got a bit cumbersome. I really need to subclass QAbstractItemModel to have a new call to extendedData() with a model identifier (and possibly type)r, and then calls to the separate models thru data(). This seems appropriate, but I was looking for gotchas, things I didn't know about with that kind of subclass.

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

    Default Re: View using multiple models

    Quote Originally Posted by johnmauer View Post
    I have been working in that direction. However, data() takes Qt:isplayRole to return the appropriate data. If I implement multiple responses, I need to use Qt::UserRoles(s) which got a bit cumbersome.
    Hmm... either I don't understand your problem or you are wrong

    Do you want a join of two models (2 columns + 2 columns = 4 columns) or an overlay of one over the other (2 columns + 2 columns = 2 columns)?
    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.


  7. #7
    Join Date
    Dec 2009
    Posts
    47
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: View using multiple models

    Ah, I didn't describe it very well. I need to overlay two sets of data from different sources. I already have one set of data, a column of doubles, in a QTableView using a subclass of QAbstractTableModel. I have a proxy model of that data to force a calculation of the data bins for a histogram plot. I now need to overlay a mathematical curve on top of the histogram, but the user can change the parameters for that curve. I subclassed a model from QAbstractItemModel to generate a lookup table from that curve and now need to merge it with the proxy model from the data in order to do the overlay.

    I can see several possible ways to do this: use Qt::UserRole types to ask for the different models from data() OR hardcode the QModelIndexes to represent specific model responses OR override all the interface methods from the view to separate the models OR .. The top level proxy model needs to identify the underlying model in some way. However, I thought that Qt might have a standard way to merge models which was my reason for the question.

    Sorry for the lack of detailed understanding. I've used another framework (MFC) for awhile. I would include a link to an example plot, but I didn't think that was appropriate. Thanks for your help.

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

    Default Re: View using multiple models

    But why do you need access to both models from outside the proxy model? If those are two separate models then don't try to merge them. Are you using a custom view or a default one? Could you craft an example or a mockup screenshot of what you want to achieve?
    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.


  9. #9
    Join Date
    Dec 2009
    Posts
    47
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: View using multiple models

    I am building a custom view to duplicate the attached graphic. The user can change the data used for the histogram, and the parameters of the curve in other views, so the views need to be synced. I had hoped to subclass the QAbstractItemView class to take advantage of the automatic notification, but that appears to need the notifications to be funneled through one model. If I built my own view, but kept the models, then I would need to implement the same connections. And I am unsure of all the connections between the model and the view.

    Please note that, although I have simplified this graphic to just one mathematical curve, the plot could have a dozen such curves. And all of them can be updated by the users elsewhere.
    Attached Images Attached Images

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

    Default Re: View using multiple models

    Ok, you have basically two choices. Either implement a proxy model where you use custom roles (Qt::UserRole and up) to return data from the "second" model or make a proxy where your curves will become additional columns in the proxy model (compared to the source model). From what I see you main model has only one column, so curves can occupy subsequent ones.

    One way or the other you will end up with a single model where two source models are represented either by different roles or by different columns.
    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.


  11. The following user says thank you to wysota for this useful post:

    johnmauer (16th January 2010)

  12. #11
    Join Date
    Dec 2009
    Posts
    47
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: View using multiple models

    The approach using different columns is excellent; I had forgotten about the extra column parameter. Thanks a lot.

  13. #12
    Join Date
    Aug 2010
    Posts
    1
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: View using multiple models

    Good day. Could you tell me how to add extra columns to the proxy model, mentioned above? I tried insertColumn() in my QSortFilterProxyModel subclass but it didn't work (returned false).
    Or is it a matter of overriding columnCount() and data() to return the data from the second model in a new column?
    Thanks in advance.

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

    Default Re: View using multiple models

    Quote Originally Posted by QtQuery View Post
    Or is it a matter of overriding columnCount() and data() to return the data from the second model in a new column?
    Yes, that's correct - you need to reimplement at least columnCount(), index(), parent() and data() to take extra columns into account.
    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.


  15. The following user says thank you to wysota for this useful post:

    QtQuery (31st August 2010)

  16. #14
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: View using multiple models

    Hello,

    i've the same problem, i need to merge 2 tree models into one treeview.
    The first model is a simple QStandardItemModel. The second model is a QDirModel.
    Now i want to display both models into one treeview. So from the posting i think i need an overlay...

    Qt Code:
    1. CustomModel Items
    2. Custom Item 1 (QStandardItemModel)
    3. Custom SubItem 1 (QStandardItemModel)
    4. Custom SubItem 2 (QStandardItemModel)
    5. Custom Item 2 (QStandardItemModel)
    6. Custom SubItem 1 (QStandardItemModel)
    7. Custom SubItem 2 (QStandardItemModel)
    8. DirModel Items
    9. C: (QDirModel)
    10. Qt (QDirModel)
    11. 2010.05 (QDirModel)
    To copy to clipboard, switch view to plain text mode 

    So, i think i need two functions setCustomModel(QAbstractItemModel *pModel); and a function setDirModel(QAbstractItemModel *pModel); for example.
    But how can i handle it that when i click on an item in the QDirModel to display a file content and if i click on an item in the QStandardItemModel to display for example the a string? To simplify this, i want to know how the treeview knows which model must be accessed.

    I've also attached a screenshot of the current situation. In the left (empty) TreeView i will merge the right ones.

    Thanks in advance

    Best Regards
    NoRulez
    Attached Images Attached Images

Similar Threads

  1. QTreeView and multiple models
    By Daher in forum Qt Programming
    Replies: 3
    Last Post: 3rd December 2019, 20:57
  2. QTreeView with multiple models
    By Daher in forum Qt Programming
    Replies: 5
    Last Post: 18th October 2011, 10:53
  3. QSortFilterProxyModel with multiple source models
    By hoshy in forum Qt Programming
    Replies: 1
    Last Post: 26th August 2009, 20:59
  4. Replies: 3
    Last Post: 5th October 2008, 23:41
  5. Proxy models and slection models
    By amunitz in forum The Model-View Framework
    Replies: 1
    Last Post: 23rd September 2008, 14:35

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.