Solved How to get item after drag item from QListWidget Thumbnails
-
@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
???
-
@VRonin When I start to drag item from list widget then who to call the same item directly inside the " onThumbItemClicked(QListWidgetItem* item) "
-
onThumbItemClicked
Do you want to respond to a click or to a drag?
-
@VRonin on drag bcz onClick is working fine for me.
-
And do you want to know it as soon as the item is dragged or when it gets dropped somewhere else?
-
@VRonin when item is drag from listWidget
-
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 -
@VRonin Again its showing error:
-
That method should go in a subclass of
QListWidget
-
This post is deleted! -
@VRonin said in How to get item after drag item from QListWidget Thumbnails:
auto isNotDragEnabled = [this](const QModelIndex &index) {
return !isIndexDragEnabled(index);
};showing error in this line " !isIndexDragEnabled(index) "
-
Yep, sorry, corrected now
-
@VRonin Now i m getting this one error "ErrorC3867 'QModelIndex::row': non-standard syntax; use '&' to create a pointer to member" In this line " draggetItems << itemAt(index.row)); "
-
These are just typos that are easy to fix... c'mon. I just forgot
()
.index.row
should beindex.row()