Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: use custom mimedata between two windows

  1. #1
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default use custom mimedata between two windows

    HI,
    I wanted to drag & drop custom mime data between two windows (basically 2 QTreeview's in the same application), But when I use this custom mimedata I am not able to drop any data (I could see + sign while dragging in 1st window & the moment I leave the 1st winnow the + sign is changing to ForbiddenCursor & I am not able to drop data in 2nd window)

    My classs:

    Qt Code:
    1. class MyMimedata : public QMimeData
    2. {
    3. Q_OBJECT
    4. public:
    5. MyMimedata () : QMimeData() { }
    6.  
    7. bool hasFormat(const QString &mimetype) const { return QMimeData::hasFormat(mimetype); }
    8. QStringList formats() const{ return QMimeData::formats(); }
    9. QVariant retrieveData(const QString &mimetype, QVariant::Type preferredType) const { QMimeData::retrieveData(mimetype, preferredType); }
    10. };
    To copy to clipboard, switch view to plain text mode 

    I am not able to drag & drop above custome data type.
    When I use
    Qt Code:
    1. Q_DECLARE_METATYPE(MyMimedata );
    To copy to clipboard, switch view to plain text mode 
    I am getting compile time error: 'QMimeData::QMimeData(const QMimeData&)' is private

    What am I doing wrong here.... any hint ??
    Last edited by prasad_N; 4th March 2016 at 14:34. Reason: missing [code] tags
    Thanks :-)

  2. #2
    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: use custom mimedata between two windows

    Your mime data doesn't offer any custom format, at least not as far as you code snippet goes.

    Cheers,
    _

  3. #3
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: use custom mimedata between two windows

    Hi Thanks for the replay.

    Quote Originally Posted by anda_skoa View Post
    Your mime data doesn't offer any custom format, at least not as far as you code snippet goes._
    Yes & I have not showed full implementation because I do n't want to dump all the code here & make you to read all the stuff, instead what i showed you is my basic requirement (passing custom mimedata to other window).

    Actually I am holding reference to some object & i am trying to access that object in other window with the help of mime data.
    Qt Code:
    1. class MyMimedata : public QMimeData
    2. {
    3. Q_OBJECT
    4.  
    5. [B] QObject* parent;[/B]
    6.  
    7. public:
    8. MyMimedata (QObject* parent) : QMimeData() { }
    9.  
    10. bool hasFormat(const QString &mimetype) const { return QMimeData::hasFormat(mimetype); }
    11. QStringList formats() const{ return QMimeData::formats(); }
    12. QVariant retrieveData(const QString &mimetype, QVariant::Type preferredType) const { QMimeData::retrieveData(mimetype, preferredType); }
    13.  
    14. [B]QObject* getParent() { return parent; }[/B]
    15. }
    To copy to clipboard, switch view to plain text mode 
    ;

    And initialization of mime data:
    Qt Code:
    1. QMimeData* myModel::mimedata(const QModelIndexList& indexs) const // myModel is inherited from QAbstractItemModel
    2. {
    3. MyMimedata* mime = new MyMimedata (this);
    4.  
    5. return mime;
    6. }
    To copy to clipboard, switch view to plain text mode 

    I am accessing it in other view:
    Qt Code:
    1. void myView::dropEvent ( QDropEvent * event ) //My view is from QTreeView
    2. {
    3. MyMimedata* myMime = qobject_cast<MyMimedata*> (event->mimedata());
    4.  
    5. if(myMime )
    6. {
    7. myModel* myModel = qobject_cast<myModel*> (myMime ->getParent());
    8.  
    9. //I wan to have my model here
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    This is what I am trying to do, but I am not actually interested in what is the implementation (We can do more customization in side) but wanted to know how can I transfer my custom mime data to the other window (this is the basic requirement once if I can do this we can do many more things which we do with c++ class ex: holding some pointer reference).

    Thanks a lot.
    Last edited by prasad_N; 6th March 2016 at 09:46.
    Thanks :-)

  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: use custom mimedata between two windows

    Quote Originally Posted by prasad_N View Post
    Yes & I have not showed full implementation because I do n't want to dump all the code here & make you to read all the stuff, instead what i showed you is my basic requirement (passing custom mimedata to other window).
    So instead of showing the relevant code you though it would be helpful to show irrelevent code?So which of the things doesn't work?

    Does the qobject_cast fail?

    Cheers,
    _

  5. #5
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: use custom mimedata between two windows

    Quote Originally Posted by anda_skoa View Post
    So instead of showing the relevant code you though it would be helpful to show irrelevent code?So which of the things doesn't work? _
    I hope its not irrelevant code, Its relevant code just to explain what is the problem & The original code is in my replay (#3)

    Quote Originally Posted by anda_skoa View Post
    Does the qobject_cast fail? _
    drop event does not even getting called in this case. but if I change my mime data function to below, drop event is getting called, So I thought problem is with MyMimedata class.

    Qt Code:
    1. QMimeData* myModel::mimedata(const QModelIndexList& indexs) const // myModel is inherited from QAbstractItemModel
    2. {
    3. QMimeData* mime = new QMimeData(); // Or QMimedata* mime = QAbstractItemModel::mimedata(indexs);
    4.  
    5. return mime;
    6. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by prasad_N; 6th March 2016 at 13:10.
    Thanks :-)

  6. #6
    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: use custom mimedata between two windows

    Do you get the dragEnter event?
    Does the cursor indicate that dropping is allowed?

    Cheers,
    _

  7. #7
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: use custom mimedata between two windows

    Quote Originally Posted by anda_skoa View Post
    Do you get the dragEnter event?
    Does the cursor indicate that dropping is allowed?

    Cheers,
    _
    Enter event is getting called & drop event is not getting called & during dropevent the cursor becomes forbidden cursor which signifies drop not allowed.
    Thanks :-)

  8. #8
    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: use custom mimedata between two windows

    So you are saying that this works
    Qt Code:
    1. QMimeData* myModel::mimedata(const QModelIndexList& indexs) const
    2. {
    3. QMimeData* mime = new QMimeData();
    4. return mime;
    5. }
    To copy to clipboard, switch view to plain text mode 
    but this
    Qt Code:
    1. QMimeData* myModel::mimedata(const QModelIndexList& indexs) const
    2. {
    3. QMimeData* mime = new MyMimedata();
    4. return mime;
    5. }
    To copy to clipboard, switch view to plain text mode 
    doesn't?
    I.e. an empty mime data object, default construced, in both cases?

    Cheers,
    _

  9. #9
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: use custom mimedata between two windows

    Quote Originally Posted by anda_skoa View Post
    So you are saying that this works
    Qt Code:
    1. QMimeData* myModel::mimedata(const QModelIndexList& indexs) const
    2. {
    3. QMimeData* mime = new QMimeData();
    4. return mime;
    5. }
    To copy to clipboard, switch view to plain text mode 
    but this
    Qt Code:
    1. QMimeData* myModel::mimedata(const QModelIndexList& indexs) const
    2. {
    3. QMimeData* mime = new MyMimedata();
    4. return mime;
    5. }
    To copy to clipboard, switch view to plain text mode 
    doesn't?
    I.e. an empty mime data object, default construced, in both cases?

    Cheers,
    _
    Yes, absolutely..
    Thanks :-)

  10. #10
    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: use custom mimedata between two windows

    Ok, this is strange.

    Have you tried with a minimal subclass? One that only has a constructor?
    And just to be sure: you did not call QAbstractItemModel::mimedata(indexs), right?

    Cheers,
    _

  11. #11
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: use custom mimedata between two windows

    Quote Originally Posted by anda_skoa View Post
    Ok, this is strange.
    Have you tried with a minimal subclass? One that only has a constructor? _
    Sorry but which class you are talking about, QMimedata class ??


    Quote Originally Posted by anda_skoa View Post
    Ok, this is strange.
    And just to be sure: you did not call QAbstractItemModel::mimedata(indexs), right? _
    Yes, I have not called, I tried putting that statement event & no change in the results.



    Just to check: I have changed the simple tree model example & I made below changes to get the minimal example.



    Model:

    Qt Code:
    1. #ifndef TREEMODEL_H
    2. #define TREEMODEL_H
    3.  
    4. #include <QAbstractItemModel>
    5. #include <QModelIndex>
    6. #include <QVariant>
    7. #include <QDebug>
    8. #include <QMimeData>
    9.  
    10. class myMidedata : public QMimeData
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. myMidedata() : QMimeData() {}
    16. };
    17.  
    18. class TreeItem;
    19.  
    20. //! [0]
    21. class TreeModel : public QAbstractItemModel
    22. {
    23. Q_OBJECT
    24.  
    25. public:
    26. explicit TreeModel(const QString &data, QObject *parent = 0);
    27. ~TreeModel();
    28.  
    29. QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE;
    30. Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
    31. QVariant headerData(int section, Qt::Orientation orientation,
    32. int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
    33. QModelIndex index(int row, int column,
    34. const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
    35. QModelIndex parent(const QModelIndex &index) const Q_DECL_OVERRIDE;
    36. int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
    37. //bool hasChildren(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
    38.  
    39. int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
    40. QMimeData* mimeData(const QModelIndexList &indexes) const;
    41.  
    42.  
    43. private:
    44. void setupModelData(const QStringList &lines, TreeItem *parent);
    45.  
    46. TreeItem *rootItem;
    47. };
    48. //! [0]
    49.  
    50. #endif // TREEMODEL_H
    51.  
    52.  
    53.  
    54.  
    55. #include "treeitem.h"
    56. #include "treemodel.h"
    57.  
    58. #include <QStringList>
    59.  
    60. //! [0]
    61. TreeModel::TreeModel(const QString &data, QObject *parent)
    62. {
    63. QList<QVariant> rootData;
    64. rootData << "Title" << "Summary";
    65. rootItem = new TreeItem(rootData);
    66. setupModelData(data.split(QString("\n")), rootItem);
    67. }
    68. //! [0]
    69.  
    70. //! [1]
    71. TreeModel::~TreeModel()
    72. {
    73. delete rootItem;
    74. }
    75. //! [1]
    76.  
    77. //! [2]
    78. int TreeModel::columnCount(const QModelIndex &parent) const
    79. {
    80. if (parent.isValid())
    81. return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
    82. else
    83. return rootItem->columnCount();
    84. }
    85.  
    86. QMimeData *TreeModel::mimeData(const QModelIndexList &indexes) const
    87. {
    88. QAbstractItemModel::mimeData(indexes);
    89.  
    90. myMidedata* mime = new myMidedata();
    91. return mime;
    92. }
    93.  
    94. //! [2]
    95.  
    96. //! [3]
    97. QVariant TreeModel::data(const QModelIndex &index, int role) const
    98. {
    99. if (!index.isValid())
    100. return QVariant();
    101.  
    102. if (role != Qt::DisplayRole)
    103. return QVariant();
    104.  
    105. TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
    106.  
    107. return item->data(index.column());
    108. }
    109. //! [3]
    110.  
    111. //! [4]
    112. Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
    113. {
    114. if (!index.isValid())
    115. return 0;
    116.  
    117. return QAbstractItemModel::flags(index) | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
    118. }
    119. //! [4]
    120.  
    121. //! [5]
    122. QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
    123. int role) const
    124. {
    125. if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
    126. return rootItem->data(section);
    127.  
    128. return QVariant();
    129. }
    130. //! [5]
    131.  
    132. //! [6]
    133. QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
    134. const
    135. {
    136. if (!hasIndex(row, column, parent))
    137. return QModelIndex();
    138.  
    139. TreeItem *parentItem;
    140.  
    141. if (!parent.isValid())
    142. parentItem = rootItem;
    143. else
    144. parentItem = static_cast<TreeItem*>(parent.internalPointer());
    145.  
    146. TreeItem *childItem = parentItem->child(row);
    147. if (childItem)
    148. return createIndex(row, column, childItem);
    149. else
    150. return QModelIndex();
    151. }
    152. //! [6]
    153.  
    154. //! [7]
    155. QModelIndex TreeModel::parent(const QModelIndex &index) const
    156. {
    157. if (!index.isValid())
    158. return QModelIndex();
    159.  
    160. TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
    161. TreeItem *parentItem = childItem->parentItem();
    162.  
    163. if (parentItem == rootItem)
    164. return QModelIndex();
    165.  
    166. return createIndex(parentItem->row(), 0, parentItem);
    167. }
    168. //! [7]
    169.  
    170. //! [8]
    171. int TreeModel::rowCount(const QModelIndex &parent) const
    172. {
    173. // return 0;
    174. TreeItem *parentItem;
    175. if (parent.column() > 0)
    176. return 0;
    177.  
    178. if (!parent.isValid())
    179. parentItem = rootItem;
    180. else
    181. parentItem = static_cast<TreeItem*>(parent.internalPointer());
    182.  
    183. return parentItem->childCount();
    184. }
    185. //! [8]
    186.  
    187. //bool TreeModel::hasChildren(const QModelIndex &parent) const
    188. //{
    189. // TreeItem *parentItem;
    190. // if (parent.column() > 0)
    191. // return false;
    192.  
    193. // if (!parent.isValid())
    194. // parentItem = rootItem;
    195. // else
    196. // parentItem = static_cast<TreeItem*>(parent.internalPointer());
    197.  
    198. // return parentItem->childCount();
    199. //}
    200.  
    201. void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
    202. {
    203. QList<TreeItem*> parents;
    204. QList<int> indentations;
    205. parents << parent;
    206. indentations << 0;
    207.  
    208. int number = 0;
    209.  
    210. while (number < lines.count()) {
    211. int position = 0;
    212. while (position < lines[number].length()) {
    213. if (lines[number].mid(position, 1) != " ")
    214. break;
    215. position++;
    216. }
    217.  
    218. QString lineData = lines[number].mid(position).trimmed();
    219.  
    220. if (!lineData.isEmpty()) {
    221. // Read the column data from the rest of the line.
    222. QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
    223. QList<QVariant> columnData;
    224. for (int column = 0; column < columnStrings.count(); ++column)
    225. columnData << columnStrings[column];
    226.  
    227. if (position > indentations.last()) {
    228. // The last child of the current parent is now the new parent
    229. // unless the current parent has no children.
    230.  
    231. if (parents.last()->childCount() > 0) {
    232. parents << parents.last()->child(parents.last()->childCount()-1);
    233. indentations << position;
    234. }
    235. } else {
    236. while (position < indentations.last() && parents.count() > 0) {
    237. parents.pop_back();
    238. indentations.pop_back();
    239. }
    240. }
    241.  
    242. // Append a new item to the current parent's list of children.
    243. parents.last()->appendChild(new TreeItem(columnData, parents.last()));
    244. }
    245.  
    246. ++number;
    247. }
    248. }
    To copy to clipboard, switch view to plain text mode 




    view :
    Qt Code:
    1. #ifndef TREEVIEW_H
    2. #define TREEVIEW_H
    3.  
    4. #include <QTreeView>
    5. #include <QDebug>
    6. #include <QDropEvent>
    7.  
    8.  
    9. class treeView : public QTreeView
    10. {
    11. Q_OBJECT
    12. public:
    13. treeView(QString title, QWidget *parent = 0);
    14. ~treeView();
    15.  
    16. QString m_title;
    17.  
    18. void dragEnterEvent(QDragEnterEvent *event);
    19. void dragMoveEvent(QDragMoveEvent *event);
    20. void dragLeaveEvent(QDragLeaveEvent *event);
    21.  
    22. void dropEvent(QDropEvent *event);
    23. };
    24.  
    25. #endif // TREEVIEW_H
    26.  
    27.  
    28. #include "treeview.h"
    29.  
    30. treeView::treeView(QString title, QWidget *parent) : QTreeView(parent), m_title(title)
    31. {
    32. setDragEnabled(true);
    33. setAcceptDrops(true);
    34. setDragDropMode(DragDrop);
    35. }
    36.  
    37. treeView::~treeView()
    38. {
    39.  
    40. }
    41.  
    42. void treeView::dragEnterEvent(QDragEnterEvent *event)
    43. {
    44. //qDebug() << "Enter event of = " << m_title;
    45. return QTreeView::dragEnterEvent(event);
    46. }
    47.  
    48. void treeView::dragMoveEvent(QDragMoveEvent *event)
    49. {
    50. return QTreeView::dragMoveEvent(event);
    51. }
    52.  
    53. void treeView::dragLeaveEvent(QDragLeaveEvent *event)
    54. {
    55. return QTreeView::dragLeaveEvent(event);
    56. }
    57.  
    58. void treeView::dropEvent(QDropEvent *event)
    59. {
    60. event->acceptProposedAction();
    61. qDebug() << "Enter event of = " << m_title;
    62. QTreeView::dropEvent(event);
    63. }
    To copy to clipboard, switch view to plain text mode 



    Qt Code:
    1. #include "treemodel.h"
    2. #include "treeview.h"
    3.  
    4. #include <QApplication>
    5. #include <QFile>
    6. #include <QTreeView>
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. Q_INIT_RESOURCE(simpletreemodel);
    11.  
    12. QApplication app(argc, argv);
    13.  
    14. QFile file(":/default.txt");
    15. file.open(QIODevice::ReadOnly);
    16. TreeModel model(file.readAll());
    17. file.close();
    18.  
    19. treeView view("view1");
    20. view.setModel(&model);
    21. view.setWindowTitle(QObject::tr("Simple Tree Model"));
    22. view.show();
    23.  
    24. treeView view1("view2");
    25. view1.setModel(&model);
    26. view1.setWindowTitle(QObject::tr("Simple Tree Model"));
    27. view1.show();
    28.  
    29.  
    30. return app.exec();
    31. }
    To copy to clipboard, switch view to plain text mode 



    I have not changed the treeitem class. & this is also not working when I try drag items from view1 to view2 or in other way .
    Thanks :-)

  12. #12
    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: use custom mimedata between two windows

    Can you also post the code that gets the dropEvent called?

    Cheers,
    _

  13. #13
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: use custom mimedata between two windows

    Quote Originally Posted by anda_skoa View Post
    Can you also post the code that gets the dropEvent called?_

    Qt Code:
    1. QMimeData *TreeModel::mimeData(const QModelIndexList &indexes) const
    2. {
    3. //This is working fine
    4. QMimeData* mime = QAbstractItemModel::mimeData(indexes);
    5. return mime;
    6.  
    7.  
    8. //Just now realized below both cases are not working in the simplified example. but QMimeData* mime = new QMimeData();
    9. is working in my project, strange
    10. //myMidedata* mime = new myMidedata();
    11. //QMimeData* mime = new QMimeData();
    12.  
    13.  
    14. //this is also not working
    15. //QAbstractItemModel::mimeData(indexes);
    16. //myMidedata* mime = new myMidedata(); OR QMimeData* mime = new QMimeData();
    17.  
    18. return mime;
    19. }
    To copy to clipboard, switch view to plain text mode 
    Thanks :-)

  14. #14
    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: use custom mimedata between two windows

    I asked you twice(!) if it worked for an empty QMimeData.
    Twice(!) you wrote that, yes, that was the case.

    So, why are you posting questions if you ignore any attempt in helping you?

    Anyway, the problem is obviously not your subclass.

    Cheers,
    _

  15. #15
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: use custom mimedata between two windows

    Quote Originally Posted by anda_skoa View Post
    I asked you twice(!) if it worked for an empty QMimeData.
    Twice(!) you wrote that, yes, that was the case.
    So, why are you posting questions if you ignore any attempt in helping you?_
    I think you misunderstood, It worked in my project where I have huge code & I can n't put all that code here so, I just created simplified version from simple tree model example just to check & I realized its not working in this simplified version.
    When you stress on that particular statement I got you point & I double checked it in my project .

    Quote Originally Posted by anda_skoa View Post
    Anyway, the problem is obviously not your subclass._
    then where is the problem, exactly this is what i am asking.. can't I pass customized mimedata OR is there a problem with my mimedata subclass OR do I need to re implement any other functions in subclass/model/view when I subclass mimedata ?
    Last edited by prasad_N; 8th March 2016 at 07:42.
    Thanks :-)

  16. #16
    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: use custom mimedata between two windows

    Quote Originally Posted by prasad_N View Post
    I think you misunderstood, It worked in my project where I have huge code & I can n't put all that code here so, I just created simplified version from simple tree model example just to check & I realized its not working in this simplified version.
    Well, yes, which is why I was specifically asking for whether two very concrete code options would show different behavior (comment #8) to which you replied
    Quote Originally Posted by prasad_N View Post
    Yes, absolutely..
    Quote Originally Posted by prasad_N View Post
    When you stress on that particular statement I got you point & I double checked it in my project .
    Luckly for you I assumed you were really that lazy not to actually check it so I made you post the code you had.

    Quote Originally Posted by prasad_N View Post
    then where is the problem, exactly this is what i am asking.. can't I pass customized mimedata OR is there a problem with my mimedata subclass OR do I need to re implement any other functions in subclass/model/view when I subclass mimedata ?
    Well, since you can't use an empty QMimeData either, the problem is most likely the emptiness.
    Which, if you think about it, is quite obvious. If there is nothing to drop, why call the drop method?

    So you either put something into your mime data object or you use the conventional creation method and get the model through different means at the drop location.
    E.g. through the QDropEvent::source().

    Cheers,
    _

  17. #17
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: use custom mimedata between two windows

    Quote Originally Posted by anda_skoa View Post
    Well, yes, which is why I was specifically asking for whether two very concrete code options would show different behavior (comment #8) to which you replied _
    they were not actually when I tested in project & when I tested on my windows version with simplified example I could see its not working, that is the reason for my answer.

    Quote Originally Posted by anda_skoa View Post
    Luckly for you I assumed you were really that lazy not to actually check it so I made you post the code you had._
    Not actually, But luckly you can assume what ever you want.

    Quote Originally Posted by anda_skoa View Post
    Well, since you can't use an empty QMimeData either, the problem is most likely the emptiness.
    Which, if you think about it, is quite obvious. If there is nothing to drop, why call the drop method?
    So you either put something into your mime data object or you use the conventional creation method and get the model through different means at the drop location.
    E.g. through the QDropEvent::source()._
    In this case, I hope below code should work but still I could see same result.

    Qt Code:
    1. QMimeData *TreeModel::mimeData(const QModelIndexList &indexes) const
    2. {
    3. QMimeData* mime = new myMidedata();
    4. mime->setText("Dummy Data");
    5.  
    6. return mime;
    7. }
    To copy to clipboard, switch view to plain text mode 
    Thanks :-)

  18. #18
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: use custom mimedata between two windows

    If somebody looking for the solution, Not for exactly what I have asked here but If you want to pass pointer or pointers to particular objects in dragdrop.

    Model:
    Qt Code:
    1. QMimeData* myModel::mimeData(const QModelIndexList &indexes) const
    2. {
    3. QMimeData* l_mimedata = QAbstractItemModel::mimeData(indexes);
    4.  
    5. //send pointer to the view in mimedata
    6. if(this->parent())
    7. {
    8. quintptr viewPtr = reinterpret_cast<quintptr>(this->parent()); // this->parent() is view in my case, but we can pass other pointers also
    9.  
    10. QByteArray encode;
    11. QDataStream stram(&encode, QIODevice::WriteOnly);
    12. stram << viewPtr;
    13. l_mimedata->setData("application/view_pointer", encode);
    14. }
    15.  
    16. return l_mimedata;
    17. }
    To copy to clipboard, switch view to plain text mode 

    View:
    Qt Code:
    1. void myView::dropEvent(QDropEvent *event)
    2. {
    3. const QMimeData* mimedata = event->mimeData();
    4.  
    5. //Decode view pointer
    6. QByteArray decode = mimedata->data("application/view_pointer");
    7. QDataStream stream(&decode, QIODevice::ReadOnly);
    8. quintptr viewPtr = 0;
    9. stream >> viewPtr;
    10.  
    11. //viewPtr will be 0, If no one sets while draging
    12. if(viewPtr)
    13. {
    14. event->acceptProposedAction();
    15.  
    16. myView* view = reinterpret_cast<myView*>(viewPtr); // you have your pointer here
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    I think, this is kind of bruit-force way & not a safe way as we are using reinterpret_cast to get back the pointer. It is safe If we can get the pointer through customized QMimedata.

    As anda_skoa suggested QDropEvent::source() is one more way but limited to particular pointer (source pointer only);
    Thanks :-)

  19. #19
    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: use custom mimedata between two windows

    Quote Originally Posted by prasad_N View Post
    I think, this is kind of bruit-force way & not a safe way as we are using reinterpret_cast to get back the pointer. It is safe If we can get the pointer through customized QMimedata.
    You will of course still check QDropEvetn::source() to determine if this is an application internal drag as otherwise anyone can make that crashed your app or makes it cast a pointer to something that isn't there.

    Quote Originally Posted by prasad_N View Post
    As anda_skoa suggested QDropEvent::source() is one more way but limited to particular pointer (source pointer only);
    I would have assumed that, in the case of a drag from a QTreeView, that view is the source.
    Is it something different?

    Cheers,
    _

  20. #20
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: use custom mimedata between two windows

    Quote Originally Posted by anda_skoa View Post
    I would have assumed that, in the case of a drag from a QTreeView, that view is the source.
    Is it something different? _
    Yes, initially when I started I only needed view, but then I came across a situation where I need some more handlers also. So I thought of some generic approach where I can have all the handlers as part of custom mime data (may use shared pointers just to avoid crashes if that pointers got deleted) & in dropevent get all those handlers with the help of this mime data. still could not do that because I am not able to pass custom mime data.
    Thanks :-)

Similar Threads

  1. Replies: 15
    Last Post: 18th February 2016, 09:19
  2. When does Mimedata() get called?
    By TEAmerc in forum Newbie
    Replies: 4
    Last Post: 13th October 2015, 15:58
  3. Replies: 3
    Last Post: 6th March 2014, 18:54
  4. Itemview D&D mimeData
    By chocolate in forum Qt Programming
    Replies: 6
    Last Post: 19th March 2009, 15:44
  5. Drag and Drop MimeData
    By Zephro in forum Qt Programming
    Replies: 10
    Last Post: 16th May 2006, 19:20

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