views:

39

answers:

1

I really don't know if this makes sense but this is what I trying to do:

I'm doing my game's editor in QT. Currently I'm using a QStandardItemModel to store all my scene items. These items have names, position, Textures(vector of Texture which is a custom class), Animations (vector of Animation), etc.

I find it useful to have one item for row cause I can add or remove these items easily besides having them in a single place so changing this model should affect the entire app.

Now, I'm trying to do specific views for say the "Textures" of a certain item. This QTableView should show the texture's name, path etc. So, basically how can I grab the vector of Textures in the general model and fill another view without doing another model?

+2  A: 

You will want to use a QSortFilterProxy Model. Set one up like this.

QTableView *tableView = new QTableView;
MyItemModel *sourceModel = new MyItemModel(this);
QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);

proxyModel->setSourceModel(sourceModel);
proxyModel->setFilterKeyColumn(column_#_to_filter_by);
proxyModel->setFilterRegExp(a_regex_that_matches_the_item_you_want_to_display);
tableView->setModel(proxyModel);

You should be able to use one model and different proxies to setup different views.

Arnold Spence
Thanks for the code, its looking pretty clear now, exactly what I was looking for. But I have another question, what if I wanted to grab specific columns for another view. Say you have a model of persons, which have name, lastname, age, sex. And you wan't a view with name and lastname only. That proxy looks like its pretended for one column only.
Alberto Toglia
In that case you could just setup a view on your model and for each column you aren't interested in, call setColumnHidden(column_num, true).
Arnold Spence
I found another solution too. It is possible to subclass the QSortFilterProxyModel and override the filterAcceptsColumn method in which you want to return false to the columns you don't want to show. Thanks again.
Alberto Toglia