PDA

View Full Version : Bind entire class /structure to a view control



chithara
22nd August 2018, 20:13
Hi,

I have a structure consists of 5 fields each of different data type which I want to bind to a Treeview control. Even though entire structure is mapped, the Treeview should list only one field from the structure. How to do ModelView in this scenario and how can I filter out which one to display. I am expecting a approach similar to WPF (model) ,where we can bind entire class object to the view. Please help as i have no idea of how to proceed

d_stranz
23rd August 2018, 00:34
The usual solution in this case is to put a QSortFilterProxyModel between your source model and the QTreeView (in Qt land, there are "views" and "widgets", not "controls" - you need to clear your brain of the WPF and MFC poisons ;) ). You will derive a new class from QSortFilterProxyModel and reimplement the QSortFilterProxyModel::filterAcceptsRow() or filterAcceptsColumn() methods (along with the map*(), data(), rowCount(), columnCount() and probably some other methods. The filterAccepts*() functions are Boolean methods, which return true if the row / column should be displayed and false otherwise.

There is too much detail to go into here. You should take a look at the QSortFilterProxyModel documentation and at the Qt examples for basic and custom filters.

chithara
24th August 2018, 07:53
Thank you so much!

wysota
24th August 2018, 14:03
For simple cases maybe it's enough to hide unwanted columns directly in the view.

chithara
31st August 2018, 09:31
How can I show only one column if i have 6 columns?

d_stranz
31st August 2018, 17:48
How can I show only one column if i have 6 columns?

Maybe by calling QTreeView::setColumnHidden() ?