Starting Drag from QListWidget initiates Unwanted Selection
-
I have created the start of a drag action from a QListWidget. This QListWidget has selection mode ExtendedSelection, see also http://doc.qt.io/qt-4.8/qabstractitemview.html#SelectionMode-enum I would l like to keep this behavior except for the fact that it shouldn't do the selection behavior originating from drag events. How can I exclude selecting items by dragging with selection mode ExtendedSelection?
-
Hi,
What do you mean by you *you created the start of a grad action" ? Aren't you using the already implemented in Qt's item view classes ?
-
Here is the code for the QWidget where the QDrag starts
void ClassList::mousePressEvent(QMouseEvent *Event) { if (Event->button() == Qt::LeftButton) DragStartPosition = Event->pos(); QListWidget::mousePressEvent(Event); } void ClassList::mouseMoveEvent(QMouseEvent *Event) { if (Event->buttons() & Qt::LeftButton) { int Distance = (Event->pos() - DragStartPosition).manhattanLength(); if (Distance >= QApplication::startDragDistance()) performDrag(); } QListWidget::mouseMoveEvent(Event); } void ClassList::performDrag() { if (currentItem()) if (currentItem()->foreground() == Qt::black) { InstantiableItem *Current = static_cast<InstantiableItem *>(currentItem()); InstantiableMime *Mime = new InstantiableMime(Current->item()); Mime->setText(QString(Current->item()->name())); QDrag *Drag = new QDrag(this); Drag->setMimeData(Mime); Drag->setPixmap(Current->icon().pixmap(QSize(20, 20))); Drag->exec(Qt::MoveAction); } }
InstantiableItem is my subclass of QListWidgetItem and InstantiableMine is my subclass of QMimeData. ClassList is a subclass of QListWidget.
-
I'd recommend rather implementing your own list model based on the drag and drop model described here.
That will likely make your implementation simpler and cleaner.