Results 1 to 4 of 4

Thread: Two QStandardItem objects handling different attributes of same object

  1. #1
    Join Date
    Feb 2017
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Two QStandardItem objects handling different attributes of same object

    I'm a newbie with the Model/View programming of Qt and have read the Editable Tree Model Example in the Qt documentation. In this example a single object (TreeItem) encapsulates two pieces of information that later are displayed in a single row containing two columns (name and description) thanks to overriding of QModelIndex QAbstractItemModel::index() and QVariant QAbstractItemModel::data().

    I have a custom class (e.g. Foo) containing two pieces of information (Foo::m_name and Foo::m_description) that I want to display in a single row containing two columns, but instead of subclassing QAbstractItemModel I want to subclass QStandardItemModel because it has some much functionality. However, it seems I must create two QStandardItem objects, one to edit/display m_name and another to edit/display m_description. Is there some standard design scheme to manage a single Foo object in memory while having more than one QStandardItem object to handle different attributes of Foo?

  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: Two QStandardItem objects handling different attributes of same object

    No, but you can subclass QAbstractTableModel to get much of the table handling logic for free and still have your own internal data structure.

  3. #3
    Join Date
    Feb 2017
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Two QStandardItem objects handling different attributes of same object

    I just found a post in qtcentre suggested Chapter 4 of Advanced Qt Programming and lo and behold, there's a discussion of a tree subsclassing QstandardItemModel and QStandardItem where each row of the tree is made up of three QstandardItem objects handling different properties of a single object.
    The implementation source code is freely available

    Basically, one has:

    Qt Code:
    1. class myItem : public QStandardItem {
    2. public:
    3. myItem(Foo &afoo) : QStandardItem(afoo.getName()), m_foo(afoo) {
    4. m_description = new QStandardItem(afoo.getDescription());
    5. }
    6. QstandardItem *m_description; // display m_description
    7. private:
    8. Foo &m_foo;
    9. };
    To copy to clipboard, switch view to plain text mode 

    and then we insert a row of two QstandardItem in our model tree

    Qt Code:
    1. class myModel: public QStandardItemModel {
    2.  
    3. StandardItem *myModel::appendRow(QStandardItem *parent, Foo &afoo)
    4. {
    5. auto *doublet = new myItem(afoo);
    6. parent->appendRow(QList<QStandardItem*>() << doublet
    7. << double->m_description);
    8. return nameItem;
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Two QStandardItem objects handling different attributes of same object

    Sounds like you would rather want a custom model that operates on your Foo objects.

    Cheers,
    _

Similar Threads

  1. Replies: 10
    Last Post: 31st March 2015, 18:29
  2. Replies: 1
    Last Post: 31st March 2011, 11:41
  3. How to call objective-C objects from my C++ object (on OS X)
    By Markus in forum General Programming
    Replies: 1
    Last Post: 3rd March 2011, 09:06
  4. object handling issue
    By prolink007 in forum Qt Programming
    Replies: 1
    Last Post: 12th April 2010, 08:42
  5. Replies: 3
    Last Post: 9th January 2010, 16:47

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.