PDA

View Full Version : Why two clicks to select an object after drag-n-drop?



agqvis
12th April 2012, 23:49
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

Spitfire
13th April 2012, 14:54
Please edit your post and use [CODE] tags. Without them your post it's just a gibberish.

As to yout problem I think what you're seeing is related to this (http://www.qtcentre.org/threads/48318-QAbstactItemView-selection-after-moving-rows?p=217749&highlight=#post217749).
Try solution I've posted there, should help.