I have a QTreeWidget full of QTreeWidgetItems and I am dragging these items and then dropping them into a QTabWidget. Drag and drop is working perfectly. What is strange is that after I do a drag of one item from the QTreeWidget and drop it, and I go back to drag another item, I have to click *twice* to select the new item. If I do not click twice, but just click on the new item and start dragging (which is what most people do), then all items between the previous dragged item and the new item are selected and dragged over. This is happening on Linux, and I believe our QA team is seeing the same behavior on Windows.
The drag code is:
void MyTreeWidget::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
dragStartPos = event->pos();
QTreeWidget::mousePressEvent(event); // Pass it on to the parent
}
void MyTreeWidget::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton) {
int distance = (event->pos() - dragStartPos).manhattanLength();
if (distance >= QApplication::startDragDistance()) {
performDrag();
}
}
QTreeWidget::mouseMoveEvent(event);
}
void MyTreeWidget:: performDrag()
{
QList<QTreeWidgetItem*> list = selectedItems();
QString main = tr("Devices: ");
QTreeWidgetItem *item = 0;
for (int i=0; i<list.size(); i++) {
item = list.at(i);
if (item) {
QString s = tr("%1 ").arg(item->type());
main.append(s);
}
}
if (item) {
QMimeData *mimeData = new QMimeData;
mimeData->setText(main);
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setPixmap(item->icon(0).pixmap(120,120));
drag->exec(Qt::CopyAction);
}
}
The drop code is:
void MyTabWidget::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasFormat("text/plain")) {
event->setDropAction(Qt::CopyAction);
event->acceptProposedAction();
}
}
void MyTabWidget::dragMoveEvent(QDragMoveEvent *event)
{
if (event->mimeData()->hasFormat("text/plain")) {
//event->setDropAction(Qt::CopyAction); // does not matter if this line is commented out or not
event->acceptProposedAction();
}
}
void MyTabWidget::dropEvent(QDropEvent *event)
{
QString dropData = event->mimeData()->text();
event->acceptProposedAction();
...... handle the dropped data...
}
What could I be doing wrong here? Or is this a Qt bug (I doubt it)?
--abhijit
Bookmarks