我创建了一个QTableWidget子类,可以从外部文件和内部行中重放掉。在拖动一行以将其移动到不同位置时,该行的副本将显示为锚定到鼠标光标。有一种方法可以在拖动时删除锚定的行副本吗?
这是班级:
#include <QtGui>
#include <QDrag>
#include "dtablewidget.h"
#include "nofocusproxystyle.cpp"
DTableWidget::DTableWidget(QWidget *parent) : QTableWidget(parent) {
//set widget default properties:
setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
viewport()->setAcceptDrops(true); //set accept drop on viewport
setDragDropOverwriteMode(false); //set drag drop overwrite to false
setDropIndicatorShown(true); //show drop indicator on tag drop
setDragDropMode(QAbstractItemView::InternalMove); //enable internal drag drop on tablular dispaly
setSelectionBehavior(QAbstractItemView::SelectRows); //enable selection of entire row
setEditTriggers(QAbstractItemView::NoEditTriggers);//preventing editing
setAlternatingRowColors(true);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
setShowGrid(false);
setAcceptDrops(true);
setWordWrap(false);
setStyleSheet("selection-background-color: yellow;"
"selection-color: #002041;"
"font-size: 75%;"
);
setStyle(new NoFocusProxyStyle(style()));
}
void DTableWidget::dragEnterEvent(QDragEnterEvent *event) {
event->acceptProposedAction();
}
void DTableWidget::dragMoveEvent(QDragMoveEvent *event) {
event->acceptProposedAction();
}
void DTableWidget::dropEvent(QDropEvent *event) {
event->acceptProposedAction();
if (event->mimeData()->urls().size() > 0) {
emit dropped(event->mimeData());
}
else {
QPoint old_coordinates = QPoint(-1,-1);
if(currentItem() != NULL) //Check if user is not accessing empty cell
{
old_coordinates = QPoint(currentItem()->row(), currentItem()->column());
}
QTableWidget::dropEvent(event);
if(this->itemAt(event->pos().x(), event->pos().y()) != NULL && old_coordinates != QPoint(-1, -1))
{
emit moved(old_coordinates.x(), itemAt( event->pos().x(), event->pos().y())->row());
}
}
}
void DTableWidget::dragLeaveEvent(QDragLeaveEvent *event) {
event->accept();
}
void DTableWidget::keyPressEvent(QKeyEvent *event) {
emit keyboard(event);
}
答案 0 :(得分:1)
覆盖x.foo
,不要在QMimeData中设置图像。您可以通过查看https://code.woboq.org/qt5/qtbase/src/widgets/itemviews/qabstractitemview.cpp.html#_ZN17QAbstractItemView9startDragE6QFlagsIN2Qt10DropActionEE