Results 1 to 8 of 8

Thread: store duplicate QStandardItem in model

  1. #1
    Join Date
    Jul 2015
    Posts
    12
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default store duplicate QStandardItem in model

    Is it possible to store a duplicate of a QStandardItem in a QStandardItem model so that when the data of one item is changed the data of the other item or items also changes? (I am not talking about multiple view windows).

    If not, what is the best way to store an index (QPersistantModelIndex?) so that I can write my own script to update a QStandardItem on change event using the signal itemChanged (I guess?).

    Would I store this index in a column of the model, or would I use a container?

    Do I need to store it at all?

    cheers

    Qt Code:
    1.  
    2. A
    3. |_1(0)(1)(2) <--
    4. |_2(0)(1)(2)
    5.  
    6. B
    7. |_3(0)(1)(2)
    8. |_4(0)(1)(2)
    9.  
    10. C
    11. |_1(0)(1)(2) <-- C1(0) or (1) or (2) needs to automatically update when A1(0) or (1) or (2) is changed.
    12. |_5(0)(1)(2)
    13. ...
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: store duplicate QStandardItem in model

    To do what you want it is best to implement a custom model.

    In your case I think it is easiest to subclass QStandardItemModel and reimplement setData(). In this method after calling the base class implementation update items related to the one being changed. You can store the dependent item pointers using custom item roles or by subclassing QStandardItem and providing API to set and query the relationship (which allows you to update them when items get destroyed).

    The most correct way (as opposed to the easiest one) is to implement a totally custom model derived from QAbstractItemModel which will embed this whole functionality in its own code.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2015
    Posts
    12
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: store duplicate QStandardItem in model

    I am currently using a QItemDelegate using setModelData(). When I create a new row I create a QList of existing items using findItems(). So it is possible to set the duplicate data for a new row where newLineNumber == oldLineNumber.

    I think I will just write a loop which will update the entire model using the same method.

    This is my third week using Qt so I wouldn't have a clue how to implement a custom model.

    But I am interested in item roles, and custom flags. I already have a script which tests Qt::ItemIsEditable and I would like to add another custom flag to this test.

    I have attempted to implement the example in my subclassed QStandardItemModel header file.

    Qt Code:
    1. class myStandardItemModel : public QStandardItemModel
    2. {
    3. Q_OBJECT
    4. public:
    5. .....
    6. enum Option {
    7. NoOptions = 0x0,
    8. myFlag1 = 0x1,
    9. myFlag2 = 0x2,
    10. //SqueezeBlank = 0x4
    11. };
    12. Q_DECLARE_FLAGS(Options, Option)
    13. ...
    14. };
    15.  
    16. Q_DECLARE_OPERATORS_FOR_FLAGS(myStandardItemModel::Options)
    To copy to clipboard, switch view to plain text mode 

    How do I set the flag?

    Qt Code:
    1. item->setFlags(myStandardItemModel::myFlag2);
    To copy to clipboard, switch view to plain text mode 

    Throws a "no matching function call" error.

    cheers

    Qt Code:
    1. C:\dev\standardItemModel\lineeditdelegate.cpp:274: error: no matching function for call to 'QStandardItem::setFlags(myStandardItemModel::Option)'
    2. item->setFlags(myStandardItemModel::myFlag2);
    3. ^
    To copy to clipboard, switch view to plain text mode 
    Last edited by qsurvae; 5th July 2015 at 12:29.

  4. #4
    Join Date
    Jul 2015
    Posts
    12
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: store duplicate QStandardItem in model

    excuse my terminology if it's not 100% spot on

    well actually i have already implemented qflags in my qstandarditemmodel subclass.

    Can I just add a custom flag to the Qt::ItemFlags so that it is testable in the existing myStandarditemModel::flags function? That's what I need.

    Qt Code:
    1. class myStandardItemModel : public QStandardItemModel
    2. {
    3. Q_OBJECT
    4. public:
    5. myStandardItemModel(int rows, int columns, QObject *parent = 0);
    6.  
    7. ~myStandardItemModel();
    8.  
    9. Qt::ItemFlags flags ( const QModelIndex &index ) const;
    10.  
    11. ....
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: store duplicate QStandardItem in model

    Quote Originally Posted by qsurvae View Post
    I am currently using a QItemDelegate using setModelData(). When I create a new row I create a QList of existing items using findItems(). So it is possible to set the duplicate data for a new row where newLineNumber == oldLineNumber.
    What if a row in the model gets deleted? What if that row is referred to in some other item?

    Qt Code:
    1. class myStandardItemModel : public QStandardItemModel
    2. {
    3. Q_OBJECT
    4. public:
    5. .....
    6. enum Option {
    7. NoOptions = 0x0,
    8. myFlag1 = 0x1,
    9. myFlag2 = 0x2,
    10. //SqueezeBlank = 0x4
    11. };
    12. Q_DECLARE_FLAGS(Options, Option)
    13. ...
    14. };
    15.  
    16. Q_DECLARE_OPERATORS_FOR_FLAGS(myStandardItemModel::Options)
    To copy to clipboard, switch view to plain text mode 

    How do I set the flag?
    Eeemm... I don't understand what you want the flag to do and where you want to set it.

    Qt Code:
    1. item->setFlags(myStandardItemModel::myFlag2);
    To copy to clipboard, switch view to plain text mode 
    No, that wouldn't make any sense

    I meant something along the lines of:

    Qt Code:
    1. class MyItem : public QStandardItem {
    2. public:
    3. using QStandardItem::QStandardItem; // C++11 (inherited constructor) but you get the picture...
    4.  
    5. ~Myitem() { foreach(QStandardItem *item, relatedToItems()) item->removeRelation(this); }
    6.  
    7. void addItemRelation(QStandardItem *item) {
    8. m_relatedItems << item;
    9. item->m_relatedToItems << this;
    10. }
    11. void removeRelation(QStandardItem *item) {
    12. m_relatedItems.remove(item);
    13. item->m_relatedToItems.remove(this);
    14. }
    15. const QSet<QStandardItem*> relatedItems() const { return m_relatedItems; }
    16. const QSet<QStandardItem*> relatedToItems() const { return m_relatedToItems; }
    17.  
    18. private:
    19. QSet<QStandardItem*> m_relatedItems;
    20. QSet<QStandardItem*> m_relatedToItems;
    21. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class ItemModel : public QStandardItemModel {
    2. // ...
    3. public:
    4. bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) {
    5. if(!QStandardItem::setData(index, value, role)) return false; // do standard stuff
    6.  
    7. MyItem *thisItem = static_cast<MyItem*>(itemFromIndex(index);
    8. QSet<QStandardItem*> items = thisItem->relatedItems();
    9. foreach(QStandardItem *item) {
    10. MyItem *myItem = static_cast<MyItem*>(item);
    11. myItem->doCustomStuffWith(thisItem);
    12. }
    13. return true;
    14. }
    15. // ...
    16. };
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jul 2015
    Posts
    12
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: store duplicate QStandardItem in model

    Quote Originally Posted by wysota View Post
    What if a row in the model gets deleted? What if that row is referred to in some other item?
    That doesn't matter as I have simply copied the data of the previous row into the new row now.


    Quote Originally Posted by wysota View Post
    Eeemm... I don't understand what you want the flag to do and where you want to set it.
    yes I'm pretty sure I do not understand how flags work. What I want is just a simple attribute I can set on an item so that I can use a logic to perform an action or not. That is what I have been using the Qt flags for thus far.

    Thanks for your help I will look at your code.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: store duplicate QStandardItem in model

    If you just want a true/false value, then the simplest is to do:

    Qt Code:
    1. item->setData(true, Qt::UserRole+1);
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. item->data(Qt::UserRole+1).toBool();
    To copy to clipboard, switch view to plain text mode 

    But I don't know how you are going to determine which other item to update just by using a boolean flag.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following user says thank you to wysota for this useful post:

    qsurvae (6th July 2015)

  9. #8
    Join Date
    Jul 2015
    Posts
    12
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: store duplicate QStandardItem in model

    I'll have a play with it, thanks.

Similar Threads

  1. Replies: 2
    Last Post: 3rd September 2013, 07:05
  2. Replies: 9
    Last Post: 14th February 2013, 19:39
  3. Replies: 2
    Last Post: 5th May 2012, 01:25
  4. Replies: 1
    Last Post: 16th June 2010, 20:19
  5. Replies: 1
    Last Post: 3rd June 2009, 19:08

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.