I have replaced QTreeWidget with QListWidget in my minimalist example (the thread's first post):
// main.cpp
#include <QtGui>
#include "MyListWidget.h"
int main(int argc, char *argv[]) {
MyListWidget w;
w.show();
return app.exec();
}
// main.cpp
#include <QtGui>
#include "MyListWidget.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
MyListWidget w;
w.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
// MyListWidget.h
#ifndef MYLISTWIDGET_H
#define MYLISTWIDGET_H
#include <QListWidget>
{
public:
protected:
// void dragEnterEvent(QDragEnterEvent *event);
// void dragMoveEvent(QDragMoveEvent *event);
// void dropEvent(QDropEvent *event);
private:
void performDrag();
};
#endif
// MyListWidget.h
#ifndef MYLISTWIDGET_H
#define MYLISTWIDGET_H
#include <QListWidget>
class MyListWidget : public QListWidget
{
public:
MyListWidget(QWidget *parent = 0);
protected:
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
// void dragEnterEvent(QDragEnterEvent *event);
// void dragMoveEvent(QDragMoveEvent *event);
// void dropEvent(QDropEvent *event);
private:
void performDrag();
QPoint startPos;
};
#endif
To copy to clipboard, switch view to plain text mode
// MyListWidget.cpp
#include <QtGui>
#include "MyListWidget.h"
setAcceptDrops(true);
addItem("1-one");
addItem("2-two");
addItem("3-three");
addItem("4-four");
addItem("5-five");
addItem("6-six");
addItem("7-seven");
}
if (e->button() == Qt::LeftButton)
startPos = e->pos();
}
if (e->buttons() & Qt::LeftButton) {
int distance = (e->pos() - startPos).manhattanLength();
performDrag();
}
}
// void MyListWidget::dragEnterEvent(QDragEnterEvent *e) {
// }
//
// void MyListWidget::dragMoveEvent(QDragMoveEvent *event) {
// }
//
// void MyListWidget::dropEvent(QDropEvent *event) {
// }
void MyListWidget::performDrag() {
if (item) {
mimeData->setText(item->text());
drag->setMimeData(mimeData);
if (drag->exec(Qt::MoveAction) == Qt::MoveAction)
delete item;
}
}
// MyListWidget.cpp
#include <QtGui>
#include "MyListWidget.h"
MyListWidget::MyListWidget(QWidget *parent) : QListWidget(parent) {
setAcceptDrops(true);
addItem("1-one");
addItem("2-two");
addItem("3-three");
addItem("4-four");
addItem("5-five");
addItem("6-six");
addItem("7-seven");
}
void MyListWidget::mousePressEvent(QMouseEvent *e) {
if (e->button() == Qt::LeftButton)
startPos = e->pos();
QListWidget::mousePressEvent(e);
}
void MyListWidget::mouseMoveEvent(QMouseEvent *e) {
if (e->buttons() & Qt::LeftButton) {
int distance = (e->pos() - startPos).manhattanLength();
if (distance >= QApplication::startDragDistance())
performDrag();
}
QListWidget::mouseMoveEvent(e);
}
// void MyListWidget::dragEnterEvent(QDragEnterEvent *e) {
// }
//
// void MyListWidget::dragMoveEvent(QDragMoveEvent *event) {
// }
//
// void MyListWidget::dropEvent(QDropEvent *event) {
// }
void MyListWidget::performDrag() {
QListWidgetItem *item = currentItem();
if (item) {
QMimeData *mimeData = new QMimeData;
mimeData->setText(item->text());
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
if (drag->exec(Qt::MoveAction) == Qt::MoveAction)
delete item;
}
}
To copy to clipboard, switch view to plain text mode
This Drad and Drop example with QListWidget works fine without any dirty-fixes - no lost clicks!
So I incline to the opinion, that QTreeWidget drag and drop issue with the lost click described above is the Qt's bag, not mine.
Bookmarks