Drag-and-drop row from one table to a single cell of another table
-
Hi,
I'm getting started with drag-and-drop mechanism.
I have twoQTableView
withQStandardItemModel
. For these tables I set selection mode rows only:setSelectionBehavior(QAbstractItemView::SelectRows); setSelectionMode(QAbstractItemView::SingleSelection);
My task is:
- take row from
table1
- analyze data in each cell of the row and convert it to a sigle
QString
- set this
QString
data into one of the already existing cells oftable2
To do so I inherited from
QTableView
and I'm trying to see the text from theQMimeData
in debug mode. But it is empty in reimplementeddragMoveEvent
anddropEvent
(even I can see that I drag selected row).How can I get data from cells during drag-and-drop?
My reimplemented drag and dropTableView
:#include "tableview.h" #include <QDropEvent> #include <QMimeData> TableView::TableView(QWidget* parent) : QTableView(parent) { setDragEnabled(true); setAcceptDrops(true); setDropIndicatorShown(true); setSelectionBehavior(QAbstractItemView::SelectRows); setSelectionMode(QAbstractItemView::SingleSelection); } void TableView::dragMoveEvent(QDragMoveEvent *event){ const QMimeData *mime = event->mimeData(); QString txt = mime->text(); QString html = mime->html(); } void TableView::dropEvent (QDropEvent *event){ const QMimeData *mime = event->mimeData(); QString txt = mime->text(); QString html = mime->html(); }
- take row from
-
@Please_Help_me_D said in Drag-and-drop row from one table to a single cell of another table:
Or probably I need to simply override some of those events and set mimedata there?
all the functions you mention are on the drop site, not the drag site as you can see in the same documentation, some lines below... https://doc.qt.io/qt-5/dnd.html#dropping - so how should they help filling the mime data on the site where you want to start the drag?
-
So where do you fill the mime data?
See https://doc.qt.io/qt-5/dnd.html#dragging -
@Christian-Ehrlicher I haven't set it.
Do I need to overrideQTableView::mousePressEvent()
ifQTableView
has drag and drop events like:dragEnterEvent(QDragEnterEvent *) dragLeaveEvent(QDragLeaveEvent *) dragMoveEvent(QDragMoveEvent *) dropEvent(QDropEvent *)
? Or probably I need to simply override some of those events and set mimedata there?
-
@Please_Help_me_D said in Drag-and-drop row from one table to a single cell of another table:
Or probably I need to simply override some of those events and set mimedata there?
all the functions you mention are on the drop site, not the drag site as you can see in the same documentation, some lines below... https://doc.qt.io/qt-5/dnd.html#dropping - so how should they help filling the mime data on the site where you want to start the drag?
-
@Christian-Ehrlicher ok I'm starting to understand
I need a little time to make my mind -
I solved my problem by reimplementing
MyTableView::dropEvent(QDropEvent *event)
.
Here it is:void MyTableView::dropEvent(QDropEvent *event){ const QMimeData *mime = event->mimeData(); QStringList typeList = model()->mimeTypes(); if (typeList.isEmpty()) return; int sourceRow, sourceCol; QByteArray encodedData = mime->data(typeList[0]); // retrieve first row and col from first selectem item QDataStream stream(&encodedData, QIODevice::ReadOnly); stream >> sourceRow >> sourceCol; QTableView* sourceTableView = qobject_cast<QTableView*> (event->source()); // get tableview from item goes if (sourceTableView == nullptr) return; // as a result we have source table view, current table view, row and col of dragged item. That is enough to process dragged data and paste it to current model }