Results 1 to 5 of 5

Thread: When does Mimedata() get called?

  1. #1
    Join Date
    Sep 2015
    Location
    Ontario, Canada
    Posts
    28
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default When does Mimedata() get called?

    I'm trying to re-implement the MimeData() function on a subclassed QTreeWidget so that when I drag and drop items from it they carry some extra text data I specify with them for adding to external text docs. So far I've found that the MimeType() function gets called when a drag begins using std::cerr, but I haven't been able to find when MimeData() gets called to add the text data so that it gets received on drops. I don't want to reimplement the drag and drop operations to add this extra mimedata as I want to keep the standard selection and drag and drop behaviour of a QTreeWidget along with its standard drag behaviour. Or should I be subclassing QTreeWidgetItem and re-implementing its MimeData() function instead of QTreeWidget's?

  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: When does Mimedata() get called?

    Quote Originally Posted by TEAmerc View Post
    So far I've found that the MimeType() function gets called when a drag begins using std::cerr, but I haven't been able to find when MimeData() gets called to add the text data so that it gets received on drops.
    I am not sure I understand the question or what kind of issue you run into.
    The mimeData() is called when the drag starts and the resulting QMimeData is stored in the QDrag object that handles the Drag&Drop.

    The Drop location simply gets that QMimeData instance from the drop event.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    TEAmerc (13th October 2015)

  4. #3
    Join Date
    Sep 2015
    Location
    Ontario, Canada
    Posts
    28
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: When does Mimedata() get called?

    Thanks, I thought it was supposed to be like that, but for some reason when I start a drag I don't see the mimedata function get called, and when I do a drop I don't see the text that I've written into the mimeData output.

    SigTreeWidget is just being defined as:

    Qt Code:
    1. class SigTreeWidget : public QTreeWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit SigTreeWidget(QWidget *parent = 0);
    6.  
    7. Q_SIGNALS:
    8.  
    9. public Q_SLOTS:
    10. void SelectedSigsDnDHelper(); //Stores the TreeWidget's selected signals in a string when selection changes
    11.  
    12. private:
    13. QPoint dragStartPosition;
    14. std::string selectedSignals;
    15. QStringList mimeTypes() const;
    16. QMimeData *mimeData(const QModelIndexList &indexes) const;
    17. };
    To copy to clipboard, switch view to plain text mode 

    With the full implementation below:

    Qt Code:
    1. SigTreeWidget::SigTreeWidget(QWidget *parent) :
    2. QTreeWidget(parent)
    3. {
    4. }
    5.  
    6. void SigTreeWidget::SelectedSigsDnDHelper()
    7. {
    8. QList<QTreeWidgetItem *> itemList = this->selectedItems();
    9.  
    10. selectedSignals.clear();
    11.  
    12. while (!itemList.isEmpty())
    13. {
    14. selectedSignals += itemList.takeFirst()->text(1).toStdString() + ".";
    15. }
    16.  
    17. if (!selectedSignals.empty())
    18. selectedSignals.pop_back();
    19.  
    20. // std::cerr << "Selected Signals = " << selectedSignals << std::endl;
    21. }
    22.  
    23. QStringList SigTreeWidget::mimeTypes() const
    24. {
    25. std::cerr << "mimeTypes" << std::endl;
    26.  
    27. QStringList types;
    28. types << "application/vnd.text.list";
    29. return types;
    30. }
    31.  
    32. QMimeData *SigTreeWidget::mimeData(const QModelIndexList &indexes) const
    33. {
    34. std::cerr << "mimeData" << std::endl;
    35.  
    36. QMimeData *mimeData = new QMimeData();
    37. QByteArray encodedData;
    38.  
    39. QDataStream stream(&encodedData, QIODevice::WriteOnly);
    40.  
    41. stream << selectedSignals.data();
    42.  
    43. mimeData->setData("application/vnd.text.list", encodedData);
    44. return mimeData;
    45. }
    To copy to clipboard, switch view to plain text mode 

    When I start a drag in the compiled version I see "mimeTypes" output to console, but I never see "mimeData" is why I was wondering when mimeData() is called originally, but from your answer I still don't know why I don't see the mimeData I want being attached to the drag.

  5. #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: When does Mimedata() get called?

    Quote Originally Posted by TEAmerc View Post
    When I start a drag in the compiled version I see "mimeTypes" output to console, but I never see "mimeData" is why I was wondering when mimeData() is called originally, but from your answer I still don't know why I don't see the mimeData I want being attached to the drag.
    Your mimeData() method is not called because it does not match the signature of QTreeWidget's mimeData():
    http://doc.qt.io/qt-5/qtreewidget.html#mimeData

    Cheers,
    _

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

    TEAmerc (13th October 2015)

  7. #5
    Join Date
    Sep 2015
    Location
    Ontario, Canada
    Posts
    28
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: When does Mimedata() get called?

    Thanks for the quick reply. I've changed my mimeData() header and function definition to below, but I still don't see it getting called when I do a drag, only the mimeTypes() function:
    Header:
    Qt Code:
    1. class SigTreeWidget : public QTreeWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit SigTreeWidget(QWidget *parent = 0);
    6.  
    7. Q_SIGNALS:
    8.  
    9. public Q_SLOTS:
    10. void SelectedSigsDnDHelper();
    11.  
    12. private:
    13. QPoint dragStartPosition;
    14. std::string selectedSignals;
    15. QStringList mimeTypes() const;
    16. QMimeData * mimeData(const QList<QTreeWidgetItem *> & items) const;
    17. };
    To copy to clipboard, switch view to plain text mode 

    Function Definition:
    Qt Code:
    1. QMimeData *SigTreeWidget::mimeData(const QList<QTreeWidgetItem *> & items) const
    2. {
    3. (void)items;
    4. std::cerr << "mimeData" << std::endl;
    5.  
    6. QMimeData *mimeData = new QMimeData();
    7. QByteArray encodedData;
    8.  
    9. QDataStream stream(&encodedData, QIODevice::WriteOnly);
    10.  
    11. stream << selectedSignals.data();
    12.  
    13. mimeData->setData("application/vnd.text.list", encodedData);
    14. return mimeData;
    15. }
    To copy to clipboard, switch view to plain text mode 

    Everything else has stayed the same as my last post

    Maybe I need something else when I create the SigTreeWidget to enable drag? I've included the code where I create the SigTreeWidget below as well since maybe I'm missing something there too:

    Qt Code:
    1. // Instantiate the tree Widget
    2. signalTreeWidget = new SigTreeWidget();
    3. signalTreeWidget->setColumnCount(2);
    4.  
    5. signalTreeWidget->setHeaderHidden(true);
    6. signalTreeWidget->setColumnHidden(1, true);
    7. signalTreeWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    8. signalTreeWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
    9. signalTreeWidget->setDragEnabled(true);
    10. signalTreeWidget->setDragDropMode(QAbstractItemView::DragOnly);
    11. signalTreeWidget->expandAll();
    12.  
    13. connect(signalTreeWidget, SIGNAL(itemSelectionChanged()), signalTreeWidget, SLOT(SelectedSigsDnDHelper()));
    14. }
    To copy to clipboard, switch view to plain text mode 

    I populate this tree using the selected items in another QTreeWidget though unless you think that part is necessary to include I'll omit it here


    Added after 16 minutes:


    I found the last reason, it was because I needed the Qt 4.8 header that is

    Qt Code:
    1. QMimeData * QTreeWidget::mimeData(const QList<QTreeWidgetItem *> items) const
    To copy to clipboard, switch view to plain text mode 

    instead of

    Qt Code:
    1. QMimeData * QTreeWidget::mimeData(const QList<QTreeWidgetItem *> [B]&[/B] items) const
    To copy to clipboard, switch view to plain text mode 

    My Qt creator says it's based on Qt 5.2.1 so I thought I had to use the Qt 5 documentation but I guess I'm somehow compiling against Qt 4.8 instead.

    Anyways thanks for all the help!
    Last edited by TEAmerc; 13th October 2015 at 16:14.

Similar Threads

  1. Replies: 3
    Last Post: 6th March 2014, 18:54
  2. Destructor not called
    By satoshi in forum General Programming
    Replies: 2
    Last Post: 23rd April 2010, 13:55
  3. Why Qt is called Qt?
    By nifei in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2009, 07:40
  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

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.