42 if (event->button() == Qt::LeftButton)
44 QModelIndex index = indexAt(event->pos());
50 setCurrentIndex(index);
51 selectionModel()->select(index, QItemSelectionModel::ClearAndSelect);
56 setCurrentIndex(QModelIndex());
59 QListView::mousePressEvent(event);
65 && (event->buttons() & Qt::LeftButton)
67 >= QApplication::startDragDistance())
73 selectionModel()->select(
myPressedIndex, QItemSelectionModel::ClearAndSelect);
75 startDrag(model()->supportedDragActions());
79 QListView::mouseMoveEvent(event);
82 void startDrag(Qt::DropActions supportedActions)
override
89 else if (!selectedIndexes().isEmpty())
91 index = selectedIndexes().first();
101 QListView::startDrag(supportedActions);
105 QModelIndexList indexes;
107 QMimeData* mimeData = model()->mimeData(indexes);
114 const QFontMetrics fm(f);
117 const int w = fm.horizontalAdvance(name) + hPad * 2;
118 const int h = fm.height() + vPad * 2;
120 QPixmap pixmap(w, h);
121 pixmap.fill(Qt::transparent);
124 p.setRenderHint(QPainter::Antialiasing);
125 QColor bg = palette().color(QPalette::Mid);
129 p.drawRoundedRect(QRect(0, 0, w, h), 4, 4);
131 p.setPen(palette().color(QPalette::Text));
132 p.drawText(QRect(0, 0, w, h), Qt::AlignCenter, name);
135 auto* drag =
new QDrag(
this);
136 drag->setMimeData(mimeData);
137 drag->setPixmap(pixmap);
138 drag->setHotSpot(QPoint(w / 2, h / 2));
139 drag->exec(supportedActions, Qt::MoveAction);