Hi!
I have a problem with Drag and Drop in my application. I have added Drag and Drop to my QTreeWidget subclass (class MyTreeWidget : public QTreeWidget) and it works almost fine. The only problem I experience is - the first click on the MyTreeWidget after drag and drop action is lost. I click (after drag and drop action) on a tree element but nothing happens. The element must become selected, but it doesn't.

If I use QListWidget instead of QTreeWidget then everything is alright.

I have wrote simplest example to demonstrate this issue:

Qt Code:
  1. // main.cpp
  2. #include <QtGui>
  3. #include "MyTreeWidget.h"
  4.  
  5. int main(int argc, char *argv[]) {
  6. QApplication app(argc, argv);
  7. MyTreeWidget w;
  8. w.show();
  9. return app.exec();
  10. }
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. // MyTreeWidget.h
  2. #ifndef PROJECTLISTWIDGET_H
  3. #define PROJECTLISTWIDGET_H
  4.  
  5. #include <QTreeWidget>
  6.  
  7. class MyTreeWidget : public QTreeWidget
  8. {
  9. Q_OBJECT
  10.  
  11. public:
  12. MyTreeWidget(QWidget *parent = 0);
  13.  
  14. protected:
  15. void mousePressEvent(QMouseEvent *event);
  16. void mouseMoveEvent(QMouseEvent *event);
  17. void dragEnterEvent(QDragEnterEvent *event);
  18. void dragMoveEvent(QDragMoveEvent *event);
  19. void dropEvent(QDropEvent *event);
  20.  
  21. private:
  22. void performDrag();
  23.  
  24. QPoint startPos;
  25. };
  26.  
  27. #endif
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. // MyTreeWidget.cpp
  2. #include <QtGui>
  3. #include "MyTreeWidget.h"
  4.  
  5. MyTreeWidget::MyTreeWidget(QWidget *parent) : QTreeWidget(parent) {
  6. setAcceptDrops(true);
  7.  
  8. (new QTreeWidgetItem(this))->setText(0, "1-one");
  9. (new QTreeWidgetItem(this))->setText(0, "2-two");
  10. (new QTreeWidgetItem(this))->setText(0, "3-three");
  11. (new QTreeWidgetItem(this))->setText(0, "4-four");
  12. (new QTreeWidgetItem(this))->setText(0, "5-five");
  13. (new QTreeWidgetItem(this))->setText(0, "6-six");
  14. (new QTreeWidgetItem(this))->setText(0, "7-seven");
  15. }
  16.  
  17. void MyTreeWidget::mousePressEvent(QMouseEvent *e) {
  18. if (e->button() == Qt::LeftButton)
  19. startPos = e->pos();
  20. QTreeWidget::mousePressEvent(e);
  21. }
  22.  
  23. void MyTreeWidget::mouseMoveEvent(QMouseEvent *e) {
  24. if (e->buttons() & Qt::LeftButton) {
  25. int distance = (e->pos() - startPos).manhattanLength();
  26. if (distance >= QApplication::startDragDistance())
  27. performDrag();
  28. }
  29. QTreeWidget::mouseMoveEvent(e);
  30. }
  31.  
  32. void MyTreeWidget::dragEnterEvent(QDragEnterEvent *e) {
  33. }
  34.  
  35. void MyTreeWidget::dragMoveEvent(QDragMoveEvent *event) {
  36. }
  37.  
  38. void MyTreeWidget::dropEvent(QDropEvent *event) {
  39. }
  40.  
  41. void MyTreeWidget::performDrag() {
  42. QTreeWidgetItem *item = currentItem();
  43. if (item) {
  44. QMimeData *mimeData = new QMimeData;
  45. mimeData->setText(item->text(0));
  46. QDrag *drag = new QDrag(this);
  47. drag->setMimeData(mimeData);
  48. if (drag->exec(Qt::MoveAction) == Qt::MoveAction)
  49. delete item;
  50. }
  51. }
To copy to clipboard, switch view to plain text mode 

I also looked in what is happening with a debugger and I have found out that QTreeWidget state() member function returns DragSelectingState when I click MyTreeWidget the first time after drag and drop action. Looks like this prevents element from being selected. So I have wrote this simple dirty-fix:

Qt Code:
  1. void MyTreeWidget::mousePressEvent(QMouseEvent *e) {
  2. if (e->button() == Qt::LeftButton)
  3. startPos = e->pos();
  4. if (DragSelectingState == state()) // this two lines
  5. setState(NoState); // fix the problem
  6. QTreeWidget::mousePressEvent(e);
  7. }
To copy to clipboard, switch view to plain text mode 


But I would like to know the reason of this behavior. Is it my bug (incorrectly implemented Drag and Drop) or Qt's one. I can not reproduce this behavior with QListWidget. So it looks like it is Qt's bug.

Any ideas would be greatly appreciated!

Thanks in advance!