Results 1 to 4 of 4

Thread: drag_drop blocks drag_select, please help

  1. #1
    Join Date
    Sep 2008
    Posts
    23
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Question drag_drop blocks drag_select, please help

    hi, there
    I need to drag_select a group of icons in the qlistwidget and then drag_drop to another widget. Just by setting the properties of the qlistwidget, I can almost do that: I can drag_select and then drag on to another widget but can't drop. Then I did some search on the net and add a MyListWgt class so that the qlistwidget can promote to MyListWgt. After I did that, I can drag and drop but no longer can drag_select (I still can do multiple selections by click items individually). Please help.
    Here is the code of the MyListWgt
    Qt Code:
    1. #include "mylistwgt.h"
    2.  
    3. MyListWgt::MyListWgt(QWidget *parent): QListWidget(parent)
    4. {
    5. }
    6.  
    7.  
    8. MyListWgt::~MyListWgt()
    9. {
    10. }
    11.  
    12. QStringList MyListWgt::mimeTypes () const
    13. {
    14. QStringList qstrList;
    15. qstrList.append("text/uri-list");
    16. return qstrList;
    17. }
    18.  
    19. void MyListWgt::mouseMoveEvent(QMouseEvent *event)
    20. {
    21. if (!(event->buttons() & Qt::LeftButton)) return;
    22.  
    23. // if no item selected, return (else it would crash)
    24. if (currentItem() == NULL) return;
    25.  
    26. QDrag *drag = new QDrag(this);
    27. QMimeData *mimeData = new QMimeData;
    28.  
    29. // construct list of QUrls
    30. // other widgets accept this mime type, we can drop to them
    31. QList<QUrl> list;
    32. list.append(QUrl(currentItem()->text())); // only QUrl in list will be text of actual item
    33.  
    34. // mime stuff
    35. mimeData->setUrls(list);
    36. drag->setMimeData(mimeData);
    37.  
    38. // start drag
    39. drag->start(Qt::CopyAction | Qt::MoveAction);
    40. }
    41.  
    42. Qt::DropActions MyListWgt::supportedDropActions () const
    43. {
    44. // returns what actions are supported when dropping
    45. return Qt::CopyAction | Qt::MoveAction;
    46. }
    To copy to clipboard, switch view to plain text mode 
    Here is the header file
    Qt Code:
    1. #ifndef MYLISTWGT_H
    2. #define MYLISTWGT_H
    3.  
    4. #include <QtGui>
    5. #include <QListWidget>
    6. #include <QStringList>
    7. #include <fstream>
    8. #include <iomanip>
    9. #include <iostream>
    10.  
    11. /**
    12. @author
    13. */
    14.  
    15. using namespace std;
    16.  
    17. class MyListWgt : public QListWidget{
    18. Q_OBJECT;
    19.  
    20. public:
    21. MyListWgt(QWidget *parent = 0);
    22.  
    23. ~MyListWgt();
    24.  
    25. QStringList mimeTypes() const;
    26. Qt::DropActions supportedDropActions () const;
    27. void mouseMoveEvent(QMouseEvent *event);
    28. };
    29.  
    30. #endif
    To copy to clipboard, switch view to plain text mode 

    Thanks for your comments.

    zl2k

  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: drag_drop blocks drag_select, please help

    Why did you reimplement the mouseMove event? It's enough to reimplement QListWidget::mimeData() to provide your own mime data for items in the list.

  3. #3
    Join Date
    Sep 2008
    Posts
    23
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: drag_drop blocks drag_select, please help

    Ok, I blocked the reimplement of mouseMove event and reimplement QListWidget::mimeData() as following:
    Qt Code:
    1. QMimeData * MyListWgt::mimeData ( const QList<QListWidgetItem *> items ) {
    2. QList<QUrl> list;
    3. list.append(QUrl(currentItem()->text())); // only QUrl in list will be text of actual item
    4.  
    5. // mime stuff
    6. QMimeData *mimeData = new QMimeData;
    7. mimeData->setUrls(list);
    8. return mimeData;
    9. }
    To copy to clipboard, switch view to plain text mode 
    It did not solve the problem. The icons in the listwidget are not consistent after drap&drop (sometimes missing the caption with only icons shown, sometimes missing both). What I want is just pass the selected item info into the drop window with the items in the drag windown unchanged (like copy, but don't really duplicate the items).

  4. #4
    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: drag_drop blocks drag_select, please help

    Did you reimplement dropMimeData() as well?

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.