How to get item after drag item from QListWidget Thumbnails
-
I am trying to find the item number or position that is drag from QListWidgetItem Thumbnails.
I want to call this function when i droping the item inside viewvoid mainWindows::onThumbItemClicked(QListWidgetItem* item) { if (((obj->GetNumOfSeries()) > 0)) { for (int i = 0; i < (obj->GetNumOfSeries()); i++) { if (this->ui->m_ThumblistWidget->item(i) == item) { obj->SeriesChange(i); LoadViewer(false); } } } }
for this function calling I need to get ListWidgetItem when i select. This function is working when i will click the thumbnail item. Now, I want to call same function i will drag an item.
-
This should be useful: https://forum.qt.io/post/444126 (
QGraphicsSceneDragDropEvent
andQDropEvent
behave in the same way for this case) -
This should be useful: https://forum.qt.io/post/444126 (
QGraphicsSceneDragDropEvent
andQDropEvent
behave in the same way for this case) -
-
That is very specific to the original question.
What you really care about is the code above, up toif(!mimeReader.commitTransaction()) break;
.
At that point you have the row index inrow
and the data contained in the cell you dragged from inmodelData
@VRonin here modelData size is 4. and row is assign 2 in my code . I have write similar code for my problem.
void QvtkOpenGLWidgetdrag::dropEvent(QDropEvent *event) { const QMimeData *mimeData = event->mimeData(); QListWidgetItem *item = new QListWidgetItem; if (mimeData->hasFormat("application/x-qabstractitemmodeldatalist")) { QByteArray encoded = mimeData->data("application/x-qabstractitemmodeldatalist"); QDataStream stream(&encoded, QIODevice::ReadOnly); while (!stream.atEnd()) { int row; int col; QMap<int, QVariant> roleDataMap; stream >> row >> col >> roleDataMap; QIcon icon = roleDataMap.value(1).value<QIcon>(); m_pixmap = icon.pixmap(icon.availableSizes().first()); //test->onThumbItemClicked(item); update(); } } else { event->ignore(); } }
-
roleDataMap.value(1)
1 isQt::DecorationRole
, do you save the icon in that role in the model you are dragging from?QListWidgetItem *item = new QListWidgetItem;
is unused and leaked. What is that for?
-
@VRonin just creating "QListWidgetItem *item = new QListWidgetItem;" to pass the item from this class
-
@amarism said in How to get item after drag item from QListWidget Thumbnails:
just creating "QListWidgetItem *item = new QListWidgetItem;" to pass the item from this class
???
-
And do you want to know it as soon as the item is dragged or when it gets dropped somewhere else?
-
then you need to reimplement
void startDrag(Qt::DropActions supportedActions)
with:void startDrag(Qt::DropActions supportedActions){ QTableWidget::startDrag(supportedActions); QModelIndexList indexes = selectionModel()->selectedIndexes(); auto isNotDragEnabled = [this](const QModelIndex &index) { return !(model()->flags(index) & Qt::ItemIsDragEnabled); }; indexes.erase(std::remove_if(indexes.begin(), indexes.end(), isNotDragEnabled), indexes.end()); QVector<QListWidgetItem*> draggetItems; draggetItems.reserve(indexes.size()); for(auto& index : qAsConst(indexes)) draggetItems << itemAt(index.row())); // now draggetItems contains the list of dragged items
-
then you need to reimplement
void startDrag(Qt::DropActions supportedActions)
with:void startDrag(Qt::DropActions supportedActions){ QTableWidget::startDrag(supportedActions); QModelIndexList indexes = selectionModel()->selectedIndexes(); auto isNotDragEnabled = [this](const QModelIndex &index) { return !(model()->flags(index) & Qt::ItemIsDragEnabled); }; indexes.erase(std::remove_if(indexes.begin(), indexes.end(), isNotDragEnabled), indexes.end()); QVector<QListWidgetItem*> draggetItems; draggetItems.reserve(indexes.size()); for(auto& index : qAsConst(indexes)) draggetItems << itemAt(index.row())); // now draggetItems contains the list of dragged items
@VRonin said in How to get item after drag item from QListWidget Thumbnails:
QModelIndexList indexes = selectedIndexes();
Its showing selectIndex() is undefined.
I m added this one code for startDrag:-
void GLWidgetdrag::startDrag(Qt::DropActions supportedActions) { QListWidgetItem* item = currentItem(); QMimeData* mimeData = new QMimeData; QByteArray ba; ba = item->text().toLatin1().data(); mimeData->setData("application/x-item", ba); QDrag* drag = new QDrag(this); drag->setMimeData(mimeData); if (drag->exec(Qt::MoveAction) == Qt::MoveAction) { /*delete takeItem(row(item)); emit itemDroped();*/ } }
Still Here showing this one error " QListWidgetItem* item = currentItem(); "
-
Its showing selectIndex() is undefined.
yes, sorry, I forgot 1 call, it's
selectionModel()->selectedIndexes()
, corrected now. I have no idea what you are trying to do in your implementation -
then you need to reimplement
void startDrag(Qt::DropActions supportedActions)
with:void startDrag(Qt::DropActions supportedActions){ QTableWidget::startDrag(supportedActions); QModelIndexList indexes = selectionModel()->selectedIndexes(); auto isNotDragEnabled = [this](const QModelIndex &index) { return !(model()->flags(index) & Qt::ItemIsDragEnabled); }; indexes.erase(std::remove_if(indexes.begin(), indexes.end(), isNotDragEnabled), indexes.end()); QVector<QListWidgetItem*> draggetItems; draggetItems.reserve(indexes.size()); for(auto& index : qAsConst(indexes)) draggetItems << itemAt(index.row())); // now draggetItems contains the list of dragged items