PDA

View Full Version : mapping composite data from model to widgets



tzeentch.gm
28th September 2011, 23:31
Hi All,

I've a newb question, could use a bit of help understanding how best to use models/views, hope someone can help.

I'm working on a configuration dialog for a program and the data structure being modified is a list of "instances" (objects) each of which has a couple of properties like "name" (string), "optional" (bool), "libraries" (QStringList). I tried using QStandardItemModel for the top level list, presenting "name" as column 0, "optional" as column 1 and "libraries" as column 2 (adding individual files to QStandardItem using its appendRows method), however something seems to be missing as mapping to widgets using QDataWidgetMapper only works for simple values like the string or bool, but mapping column 2 (a QStandardItem with multiple rows) to a QListView doesn't work.

So a couple of questions:

1. is it possible to map a list value to a QListView and if so how? am I thinking in the right direction with the QStandardItem?

2. am I missing a piece of the puzzle? something like a custom delegate? what would it need to do?

3. are models supposed to encapsulate the entire data structure (e.g. in this case the top level list) and fit in and present the data as either a list, table or tree that views expect? Or are you supposed to have models for each composite data e.g. a model for top-level list of objects (presenting "name" and "optional" as columns) and a separate model for each object's list of files? How would you use that then to map to editor widgets?

Many thanks,
Michael

ChrisW67
29th September 2011, 01:53
1. is it possible to map a list value to a QListView and if so how? am I thinking in the right direction with the QStandardItem?
2. am I missing a piece of the puzzle? something like a custom delegate? what would it need to do?

Yes. The QDataWidgetMapper uses the USER property on simple controls (through a default QItemDelegate). This default mechanism will not work with a complex widget, but you can provide your own delegate derivative (provide your own setEditorData() and setModelData()) and load/unload either a model that a QListView is observing, or the QListWidget convenience widget.



3. are models supposed to encapsulate the entire data structure (e.g. in this case the top level list) and fit in and present the data as either a list, table or tree that views expect? Or are you supposed to have models for each composite data e.g. a model for top-level list of objects (presenting "name" and "optional" as columns) and a separate model for each object's list of files? How would you use that then to map to editor widgets?

I would try to do it with a single model if the library list is a simple QStringList... less to try to synchronise.

tzeentch.gm
3rd October 2011, 23:39
Thank you very much for your answer Chris and sorry for the late reply.


Yes. The QDataWidgetMapper uses the USER property on simple controls (through a default QItemDelegate). This default mechanism will not work with a complex widget, but you can provide your own delegate derivative (provide your own setEditorData() and setModelData()) and load/unload either a model that a QListView is observing, or the QListWidget convenience widget.

Thanks for the pointers, I'm reading up on using delegates now. One thing that keeps bugging me is I keep getting lost in how these pieces are supposed to fit together. So on the QDataWidgetMapper I set the model to the main model and add a mapping on column 2 (the one with a QStandardItem with multiple rows) to a QListView; now the first question is what model does this QListView use? AFAICS, QDataWidgetMapper doesn't set a model for the widget.

If I create a custom delegate and set it on the QDataWidgetMapper, what should this delegate do? Should it maintain some sort of private model for the QListView? Or rather multiple models, one for each row in the main model (and swap between them as the current index on the main model is changed)?


I would try to do it with a single model if the library list is a simple QStringList... less to try to synchronise.

That's what I thought as well.

Many thanks for your help, all the best :)

ChrisW67
4th October 2011, 01:46
You could allocate and populate a QStandardItem model to connect to a QListView, but you would have to make sure it did not become a memory leak. I'd be inclined to use QListWidget, which has a built-in model, and simply clear and load it in setEditorData(), and push the data back the other way in setModelData().

There are probably other approaches.

tzeentch.gm
4th October 2011, 07:32
I'd be inclined to use QListWidget, which has a built-in model, and simply clear and load it in setEditorData(), and push the data back the other way in setModelData().

Many thanks, I'll give that a shot :)