Results 1 to 5 of 5

Thread: QTreeView Drag and Drop with MyData

  1. #1
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QTreeView Drag and Drop with MyData

    I have QTreeView which is connected to QStandardItemModel and I would like to support D&D in my view.

    QStandardItem in my model contains my "MyData" as QVariant in Qt::UserRole like this
    Qt Code:
    1. s_SqlListInfo info;
    2. //fill data
    3. [...]
    4.  
    5. var.setValue(info);
    6. item->setData(var,Qt::UserRole);
    7. item->setData(info.queryName,Qt::DisplayRole);
    8. item->setData(QIcon(":/ListIcon.png"),Qt::DecorationRole);
    9. item->setFlags(item->flags()& ~Qt::ItemIsEditable);
    To copy to clipboard, switch view to plain text mode 

    I have declared myData like this

    Qt Code:
    1. struct s_SqlListInfo
    2. {
    3. QVariant queryName;
    4. QVariant dataBaseName;
    5. QList <QString> reqTableList;
    6. QString sqlStatement;
    7. };
    8. // Qt MetaType Declaration
    9. Q_DECLARE_METATYPE(s_SqlListInfo)
    To copy to clipboard, switch view to plain text mode 

    Now, When I start dragging on my treeView it will assert with this message
    ASSERT failure in QVariant::save "Invalid type to save", file kernel\qvariant.cpp line 1951
    It seems like QVariant can not save my "MyData" that's why its asserting .... how can I fix this problem?

    baray98

  2. #2
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTreeView Drag and Drop with MyData

    You can make your type savable by providing stream operators and calling qRegisterMetaTypeStreamOperators. It might be easier to just store the data individually, in Qt::UserRole + 1, etc, as Qt understands QVariant and QString automatically.

    EDIT: This should do it:
    Qt Code:
    1. QDataStream &operator<<(QDataStream &out, const s_SqlListInfo &myObj)
    2. {
    3. out << myObj.queryName << myObj.dataBaseName << myObj.sqlStatement << myObj.reqTableList;
    4. return out;
    5. }
    6.  
    7. QDataStream &operator>>(QDataStream &in, s_SqlListInfo &myObj)
    8. {
    9. in >> myObj.queryName >> myObj.dataBaseName >> myObj.sqlStatement >> myObj.reqTableList;
    10. return in;
    11. }
    12.  
    13. //Before use...
    14. qRegisterMetaTypeStreamOperators<s_SqlListInfo>("s_SqlListInfo");
    To copy to clipboard, switch view to plain text mode 
    Last edited by numbat; 20th August 2009 at 14:30.

  3. #3
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeView Drag and Drop with MyData

    Thanks for the reply ...

    But I am not sure where to add the above code in the project .. pardon my being newbie

    should i put it before my declaration of my struct ?

    baray98

  4. #4
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeView Drag and Drop with MyData

    Qt Code:
    1. QDataStream &operator<<(QDataStream &out, const DMUtils::s_SqlListInfo &myObj)
    2. {
    3. out << myObj.queryName << myObj.dataBaseName << myObj.sqlStatement << myObj.reqTableList;
    4. return out;
    5. }
    6.  
    7. QDataStream &operator>>(QDataStream &in, DMUtils::s_SqlListInfo &myObj)
    8. {
    9. in >> myObj.queryName >> myObj.dataBaseName >> myObj.sqlStatement >> myObj.reqTableList;
    10. return in;
    11. }
    12.  
    13. int main (int argc,char *argv[])
    14. {
    15. QApplication app(argc,argv);
    16.  
    17. qRegisterMetaTypeStreamOperators<DMUtils::s_SqlListInfo>("s_SqlListInfo");
    18. qRegisterMetaType<DMUtils::s_SqlListInfo>("s_SqlListInfo");
    19.  
    20.  
    21. DMMainWindow *mainWin = new DMMainWindow;
    22.  
    23. mainWin->show();
    24.  
    25. return app.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 

    I did it like this and it will still crash!!!!!!!!!!!!!!!!!

  5. #5
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeView Drag and Drop with MyData

    i got it thanks buddy

    the problem was this

    Qt Code:
    1. qRegisterMetaTypeStreamOperators<DMUtils::s_SqlListInfo>("s_SqlListInfo");
    2. qRegisterMetaType<DMUtils::s_SqlListInfo>("s_SqlListInfo");
    3.  
    4. //change to
    5.  
    6. qRegisterMetaTypeStreamOperators<DMUtils::s_SqlListInfo>("DMUtils::s_SqlListInfo");
    7. qRegisterMetaType<DMUtils::s_SqlListInfo>("DMUtils::s_SqlListInfo");
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to baray98 for this useful post:

    XRI-Vision (25th February 2015)

Similar Threads

  1. Preserving structure in QTreeView drag and drop
    By Tito Serenti in forum Qt Programming
    Replies: 1
    Last Post: 14th February 2009, 03:39
  2. Drag and drop in QTreeView
    By Valheru in forum Qt Programming
    Replies: 3
    Last Post: 27th July 2008, 09:36
  3. Drag and Drop on Whitespace of QTreeView
    By T1c4L in forum Qt Programming
    Replies: 6
    Last Post: 14th May 2008, 13:29
  4. Drag & drop items on the same QTreeView
    By wind in forum Qt Programming
    Replies: 2
    Last Post: 11th October 2006, 14:29
  5. Drag & drop for QTreeView
    By yogeshm02 in forum Qt Programming
    Replies: 2
    Last Post: 30th January 2006, 14:32

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.