1

Let's say i want to implement a library tool to manage different types of media. Therefore i have a base class Medium and derived classes e.g. Book and DVD which have additional properties.

My Problem is i can't figure out how to design the models correctly. I want to be able to show the media in one TableView with the basic properties from the Medium class as well as only the DVD's in another TableView with it's special properties from the derived class. And if i delete a DVD, i want it to be deleted in the Media table as well. Therefore i thought that they need to have the same data source.

What is the best way to achive this? Haven't found any example that illustrates that problem.

trixn
  • 15,761
  • 2
  • 38
  • 55

2 Answers2

0

It can be simple one model which includes all columns from all three sources - medium, dvd, book.

In one view you make only columns of "medium" to be shown, other hidden, in another view you allows to show only "dvd" columns. But model object is just one applied as source to all views.

If one row is removed from the model, all views will be updated appropriate. Same about "add".

yshurik
  • 772
  • 5
  • 12
  • i don't see how this will work if i have a single data source e.g. a QList of Medium objects in my model. If i want to only display DVDs i must make sure that other types are filtered out somehow because Books don't have "dvd" columns to display. I even don't want them to be displayed at all in the DVD list... So i need to filter not only columns to be hidden but also rows that do not contain DVD's. – trixn Jun 27 '16 at 13:59
  • If it is going to be not plain row-like model but structured like tree of dvd->books etc, then if you would like to keep datasource in one place(model) you can design tree-like model which then can be manipulated by using proxies to provide appropriate (proxy) models to views. Have a look at set of articles about models here: http://lynxline.com/category/models/, especially one about this one: http://lynxline.com/jongling-qt-models/, which turns tree-like model into plain. – yshurik Jun 27 '16 at 19:34
0

implement own Tree Item (take a look, for example: http://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html#treeitem-class-definition)

add something like enum with types, add methods for your TreeItem to manipulate with types, then use proxy model(detailed example: http://doc.qt.io/qt-5/qsortfilterproxymodel.html#details) to sort data.

like:

enum Property {Book, DVD, Other };
class TreeItem
{
...
public:
Property GetProperty() const;
void setProperty(Property iProp);
...
};
jonezq
  • 344
  • 1
  • 5
  • Thanks for your answer but i am still a bit confused. Can u precise a little how to tie everything together. While i see, how the model is structured i don't get how to get the TableView to only display a plain Table of e.g. DVD's using this model. For example how does the TableView get the headerData because it will vary depending on what i want to display. Medium objects will only have e.g. a title and an author while dvds will additionally have e.g. a length property to display additionally. Thanks in advance for your help. – trixn Jun 27 '16 at 14:12
  • do i have to reiplement headerData() in the proxymodel then, to get the header data corresponding to my subclass? and how do i filter dvds with the help of the proxy model? – trixn Jun 27 '16 at 14:20
  • just reimplement filter method in proxymodel – jonezq Jun 28 '16 at 07:08