Results 1 to 2 of 2

Thread: Reimplemented QStandardItem. Drag&Drop

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Reimplemented QStandardItem. Drag&Drop

    Hello!
    I would like to realize drag&drop with reimplemented QStandardItemModel. QStandardItem also reimplemented. It looks like:
    Qt Code:
    1. class _DLL_PREFIX_ TMyItem : public QStandardItem
    2. {
    3. public:
    4. virtual QVariant data(int role = Qt::UserRole + 1) const;
    5. virtual void setData(const QVariant &value, int role = Qt::UserRole + 1);
    6.  
    7. private:
    8. TElementData *m_pElementData;
    9. }
    10.  
    11. QVariant TMyItem::data(int role) const
    12. {
    13. if (role == Qt::UserRole+1)
    14. {
    15. return QVariant::fromValue(m_pElementData);
    16. }
    17. return QStandardItem::data(role);
    18. }
    19.  
    20. void TMyItem::setData(const QVariant &value, int role)
    21. {
    22. if (role == Qt::UserRole+1)
    23. {
    24. ASSERT(value.canConvert<TElementData *>())
    25. m_pElementData = value.value<TElementData*>()->clone();
    26. return;
    27. }
    28.  
    29. QStandardItem::setData(value, role);
    30. }
    To copy to clipboard, switch view to plain text mode 

    When I try to drag an element within the model (to reorder elements) I find out that user data is not copied (m_pElementData = NULL in the dropped element). I think I should reimplement some other functions to make all this work correctly. But I'm not quite sure what functions and in what classes. It seems to me that: QStandardItemModel::mimeData(...), maybe QStandardItemModel::itemData (...)? Something else?

    Thanks in advance.

  2. #2
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reimplemented QStandardItem. Drag&Drop

    So, maybe this will be useful for somebody. The following was done.
    TMyModel : public QStandardItemModel
    Reimplemented metods: itemData, setItemData, data, setData.
    TMyItem : public QStandardItem
    Reimplemented methods: data, setData

    Don't forget:
    Qt Code:
    1. Q_DECLARE_METATYPE(TElementData*)
    2.  
    3. qRegisterMetaType<TElementData*>("TDataPtr");
    4. qRegisterMetaTypeStreamOperators<TElementData*>("TDataPtr");
    To copy to clipboard, switch view to plain text mode 

    Realize functions:
    Qt Code:
    1. QDataStream &operator<<(QDataStream &Out, const TElementData* pElement)
    2. QDataStream &operator>>(QDataStream &In, TElementData* &pElement)
    To copy to clipboard, switch view to plain text mode 
    Last edited by Miga; 3rd July 2012 at 07:13. Reason: code improve

Similar Threads

  1. Reimplemented QStandardItem
    By Miga in forum Qt Programming
    Replies: 6
    Last Post: 3rd April 2012, 14:48
  2. Replies: 0
    Last Post: 7th January 2012, 15:20
  3. Replies: 4
    Last Post: 24th August 2011, 22:33
  4. Replies: 2
    Last Post: 13th October 2010, 21:51
  5. Replies: 0
    Last Post: 4th May 2010, 10:24

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
  •  
Qt is a trademark of The Qt Company.