PDA

View Full Version : Derived QListWidget Drag&Drop problem



razdraz
14th June 2015, 13:04
Hello,

I'm relatively new to Qt and I have problems to implement drag&drop function to my derived QListWidget class. While the drag is working perfectly, the drop does't. The cursor changes to "forbidden" after dragging an Item.
For now, I just want to get the x-Axis position of the drag and drop point (Drag is working).

Derived class:

.h:


class myQListWidget:public QListWidget
{
Q_OBJECT

public:
myQListWidget(QWidget * parent = 0);
~myQListWidget();

protected:
void dropEvent(QDropEvent *event);
void dragEnterEvent(QDragEnterEvent *event);

};


.cpp:


myQListWidget::myQListWidget(QWidget * parent)
{

this->setAcceptDrops(true);
this->setDragDropMode(QAbstractItemView::DragDrop);

}

myQListWidget::~myQListWidget()
{

}

void myQListWidget::dropEvent(QDropEvent *event)
{
xPositionDrop = event->pos().rx();
qDebug() << "xPositionDrop: " << xPositionDrag;
}

void myQListWidget::dragEnterEvent(QDragEnterEvent *event)
{
xPositionDrag = event->pos().rx();
qDebug() << "xPositionDrag: " << xPositionDrag;
}


When using the original QListWidget class, the cursor does not change to "forbidden" and drops work properly.
What am I missing here?

Thanks.

ChrisW67
14th June 2015, 20:56
Neither your dragEnterEvent() nor your dropEvent() calls acceptProposedActin() on the event.
See Dropping here: http://doc.qt.io/qt-5.4/dnd.html
There is more item view drag and drop information here: http://doc.qt.io/qt-5.4/model-view-programming.html#using-drag-and-drop-with-item-views

razdraz
15th June 2015, 16:25
The event->acceptProposedAction() thing makes it work ;-) thanks