2025-06-17 17:20:54 +08:00
|
|
|
#include "cusTableWidget.h"
|
2025-06-18 10:19:54 +08:00
|
|
|
|
|
|
|
|
#include <QApplication>
|
2025-06-17 17:20:54 +08:00
|
|
|
#include <QDrag>
|
|
|
|
|
#include <QMimeData>
|
|
|
|
|
#include <QPainter>
|
|
|
|
|
|
2025-06-18 10:19:54 +08:00
|
|
|
CustomTableWidget::CustomTableWidget(QWidget* parent) : QTableWidget(parent)
|
2025-06-17 17:20:54 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CustomTableWidget::~CustomTableWidget()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CustomTableWidget::dropEvent(QDropEvent* event)
|
|
|
|
|
{
|
|
|
|
|
if (!event->mimeData()->hasFormat("application/x-custom-data")) {
|
|
|
|
|
event->ignore();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-06-18 10:19:54 +08:00
|
|
|
QTableWidget::dropEvent(event);
|
2025-06-17 17:20:54 +08:00
|
|
|
}
|
|
|
|
|
|
2025-06-18 10:19:54 +08:00
|
|
|
void CustomTableWidget::dragEnterEvent(QDragEnterEvent* event)
|
2025-06-17 17:20:54 +08:00
|
|
|
{
|
|
|
|
|
const QTableWidget* source = qobject_cast<const QTableWidget*>(event->source());
|
|
|
|
|
if (source == this) {
|
|
|
|
|
event->ignore();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (event->mimeData()->hasFormat("application/x-custom-data")) {
|
|
|
|
|
event->acceptProposedAction();
|
|
|
|
|
} else {
|
|
|
|
|
event->ignore();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-18 10:19:54 +08:00
|
|
|
void CustomTableWidget::mouseMoveEvent(QMouseEvent* event)
|
2025-06-17 17:20:54 +08:00
|
|
|
{
|
2025-06-18 10:19:54 +08:00
|
|
|
if (!(event->buttons() & Qt::LeftButton)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ((event->pos() - startPos_).manhattanLength() < QApplication::startDragDistance()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-17 17:20:54 +08:00
|
|
|
QDrag* drag = new QDrag(this);
|
|
|
|
|
QMimeData* mimeData = new QMimeData;
|
|
|
|
|
|
|
|
|
|
QStringList selectedTexts;
|
|
|
|
|
foreach (QTableWidgetItem* item, selectedItems()) {
|
2025-06-18 10:19:54 +08:00
|
|
|
if (item->column() == 1) {
|
|
|
|
|
selectedTexts << item->text();
|
|
|
|
|
}
|
2025-06-17 17:20:54 +08:00
|
|
|
}
|
2025-06-18 10:19:54 +08:00
|
|
|
mimeData->setData("application/x-custom-data", selectedTexts.join("|").toUtf8());
|
2025-06-17 17:20:54 +08:00
|
|
|
drag->setMimeData(mimeData);
|
|
|
|
|
QPixmap pixmap(100, 50);
|
|
|
|
|
pixmap.fill(Qt::lightGray);
|
|
|
|
|
QPainter painter(&pixmap);
|
|
|
|
|
painter.drawText(pixmap.rect(), Qt::AlignCenter, QString("%1 ITEM").arg(selectedTexts.count()));
|
|
|
|
|
drag->setPixmap(pixmap);
|
2025-06-18 10:19:54 +08:00
|
|
|
drag->exec(Qt::CopyAction | Qt::MoveAction);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CustomTableWidget::mousePressEvent(QMouseEvent* event)
|
|
|
|
|
{
|
|
|
|
|
QTableWidget::mousePressEvent(event);
|
|
|
|
|
if (event->button() == Qt::LeftButton) {
|
|
|
|
|
startPos_ = event->pos();
|
|
|
|
|
}
|
2025-06-17 17:20:54 +08:00
|
|
|
}
|