You are not logged in.

sparcito

Beginner

  • "sparcito" is male
  • "sparcito" started this thread

Posts: 25

Location: Strasbourg, France

  • Send private message

1

Wednesday, September 21st 2011, 8:57am

Question about Model & Views : using the same model in a QListView and a QTreeView...

Hello to all,

I have a question related to Model & View concept in Qt4.
I'm taking over some "older" source code and I have to perform some upgrades.

I have a software module that allows selecting a scenario from a list and starting this scenario.
The user interface of this module is a plugin which takes a QStandardItemModel.

We currently have two different plugins which implement different user interfaces views:
- Plugin 1 : displays a list of scenario names in a QListView
- Plugin 2 : displays a list of scenario names in a QTreeView

Until now, the displayed information was the same.

Today, I have to upgrade the second plugin to add other information, in the following order:
- scenario identifier
- scenario name
- scenario description
- scenario date

Notice that in the second plugin, for each scenario element, it shall be possible to display sub-scenarios (items) in the QTreeView.

Therefore we would have e.g.:

Plugin 1: (QListView)

Source code

1
2
3
Scenario 1 
Scenario 2 
Scenario 3


Plugin 2: (QTreeView)

Source code

1
2
3
4
5
6
7
+ 1 Scenario 1 My first scenario 2011/09/21 
│ ├ 1 Sub-scenario 1 My first sub-scenario 2011/09/21 
│ ├ 2 Sub-scenario 2 My second sub-scenario 2011/09/21 
│ └ 3 Sub-scenario 3 My third sub-scenario 2011/09/21 
├ 2 Scenario 2 My second scenario 2011/09/21 
│ └ 1 Sub-scenario 4 My fourth sub-scenario 2011/09/21 
└ 3 Scenario 3 My third scenario 2011/09/21



All the scenario information is stored in the model as items defined as children of the invisible root item (the sub-scenario items are defined as children of the "main" scenario items).

By now, the scenario name was set in the first "column" so that it gets displayed in both plugins (the QListView only displays one column).

The question is now to find how to get the display above:
- by default if I want to have the scenario name displayed in the Plugin 1 I have to put it in the first column (QListView);
- if I put the scenario name in the first column, I have to set the sub-scenario items as children of the scenario name information (and not the scenario identifier), unless they don't get displayed in the QTreeView.

I also tried to swap the two first columns in Plugin 2, but I cannot figure out when I could do this (if I perform it just after having set the model on the view nothing happens).
I also thought something was possible using QAbstractProxyModel, but I'm really lost there.
Would a better solution to use a QTreeView in the Plugin 1 and to hide the unwanted columns?

Could someone help me?

Many thanks in advance.

sparcito

Junior

Professional

  • "Junior" is male

Posts: 1,623

Location: San Antonio, TX USA

Occupation: Senior Secure Systems Engineer

  • Send private message

2

Wednesday, September 21st 2011, 7:16pm

If:
col 1 - scenario identifier
col 2 - scenario name
col 3 - scenario description
col 4 - scenario date

listView->setModelColumn( 1 );

This will only display column 2 scenario name in the listview (not the children).

sparcito

Beginner

  • "sparcito" is male
  • "sparcito" started this thread

Posts: 25

Location: Strasbourg, France

  • Send private message

3

Friday, September 23rd 2011, 9:29am

That's exactly what I was looking for!

Many thanks! :)