Results 1 to 5 of 5

Thread: mapping composite data from model to widgets

  1. #1
    Join Date
    Sep 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default mapping composite data from model to widgets

    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

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: mapping composite data from model to widgets

    Quote Originally Posted by tzeentch.gm View Post
    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.

  3. #3
    Join Date
    Sep 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: mapping composite data from model to widgets

    Thank you very much for your answer Chris and sorry for the late reply.

    Quote Originally Posted by ChrisW67 View Post
    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)?

    Quote Originally Posted by ChrisW67 View Post
    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

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: mapping composite data from model to widgets

    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.

  5. #5
    Join Date
    Sep 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: mapping composite data from model to widgets

    Quote Originally Posted by ChrisW67 View Post
    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

Similar Threads

  1. Proxy model, index mapping.
    By wojtekw in forum Qt Programming
    Replies: 14
    Last Post: 7th January 2016, 08:09
  2. Replies: 9
    Last Post: 14th February 2013, 19:39
  3. Class for mapping input widgets
    By Hostel in forum Newbie
    Replies: 2
    Last Post: 13th September 2011, 09:21
  4. Replies: 1
    Last Post: 24th February 2011, 05:54
  5. Creating And Destoying composite Widgets
    By morraine in forum Newbie
    Replies: 3
    Last Post: 31st August 2008, 16:57

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.