我正在制作一个使用QTableWidget的项目。当我试图让它接受滴剂时,我开始知道它不像我想的那样表现。问题是,如果我将QTableWidget更改为QWidget,那么drop就被接受了。所以,问题不在于编码。这里是代码,至于我关注的问题是在“MyDropWidget”类
中#include <QMouseEvent>
#include <QWidget>
#include <QMessageBox>
#include <math.h>
#include <QApplication>
#include <QPainter>
#include <sstream>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QLabel>
#include <QPaintEvent>
#include <QTableWidget>
#define RADIUS 2
#define DISTANCE_BETWEEN_OBJECTS 2
#define DASH_RECT_HALF_WIDTH 2
#include <QHeaderView>
#include <QPainter>
#include <QHBoxLayout>
#include <QWidget>
#include <QApplication>
class MyMessageBox:public QMessageBox
{
public:
MyMessageBox(std::string message,QWidget *parent=0):QMessageBox(QMessageBox::NoIcon,QString("ErrorMessage"),QString(message.c_str()),QMessageBox::Ok,parent,Qt::Widget)
{
}
};
class MyDragWidget:public QWidget
{
private:
QPoint * start_Pos;
QPixmap drag_Pixmap;
public:
MyDragWidget(QWidget * parent);
private:
void mousePressEvent(QMouseEvent * event);
void mouseReleaseEvent(QMouseEvent * event);
void mouseMoveEvent(QMouseEvent * event);
};
MyDragWidget::MyDragWidget(QWidget * parent):QWidget(parent)
{
setPalette(QPalette(QColor(0,0,0)));
setAutoFillBackground(true);
start_Pos = NULL;
drag_Pixmap = QPixmap(50,50);
QPainter painter(&drag_Pixmap);
painter.setPen(QColor(255,0,0));
painter.drawText(0,0,50,50,Qt::AlignCenter,"drag Pic");
painter.end();
resize(100,200);
}
void MyDragWidget::mousePressEvent(QMouseEvent *event)
{
QWidget::mousePressEvent(event);
if(event->button() == Qt::LeftButton)
{
if(start_Pos)
delete start_Pos;
start_Pos = new QPoint(event->pos());
}
}
void MyDragWidget::mouseReleaseEvent(QMouseEvent *event)
{
if(start_Pos)
delete start_Pos;
start_Pos = NULL;
}
void MyDragWidget::mouseMoveEvent(QMouseEvent *event)
{
if(!(event->buttons() & Qt::LeftButton))
{
MyMessageBox mb("mouse move 0");
mb.exec();
return;
}
if(!start_Pos)
return;
QDrag * drag = new QDrag(this);
drag->setPixmap(drag_Pixmap);
QMimeData * mimeData = new QMimeData();
mimeData->setText("name");
drag->setMimeData(mimeData);
Qt::DropAction dropAction = drag->exec(Qt::CopyAction,Qt::MoveAction);
}
class MyDropWidget:public QTableWidget
{
public:
MyDropWidget(QWidget * parent);
private:
void dragEnterEvent(QDragEnterEvent * event);
void dropEvent(QDropEvent *event);
};
MyDropWidget::MyDropWidget(QWidget * parent):QTableWidget(parent)
{
setColumnCount(6);
setRowCount(9);
setAcceptDrops(true);
setPalette(QPalette(QColor(250,150,210)));
setAutoFillBackground(true);
}
void MyDropWidget::dragEnterEvent(QDragEnterEvent * event)
{
if(event->mimeData()->hasFormat("text/plain"))
event->acceptProposedAction();
QTableWidget::dragEnterEvent(event);
}
void MyDropWidget::dropEvent(QDropEvent * event)
{
QString shape = event->mimeData()->text();
MyMessageBox mm("Drop ");
mm.exec();
QTableWidget::dropEvent(event);
}
class Main_Widget:public QWidget
{
MyDragWidget * courses_DragWidget;
MyDragWidget * rooms_DragWidget;
MyDropWidget * timeTable_Widget;
public:
Main_Widget();
};
Main_Widget::Main_Widget()
{
courses_DragWidget = new MyDragWidget(NULL);
timeTable_Widget = new MyDropWidget(NULL);
QHBoxLayout * hBoxLayout = new QHBoxLayout;
hBoxLayout->addWidget(courses_DragWidget,1);
hBoxLayout->addWidget(timeTable_Widget,2);
setLayout(hBoxLayout);
}
int main(int argc,char * argv[])
{
QApplication app(argc,argv);
Main_Widget * main_Widget = new Main_Widget;
main_Widget->show();
main_Widget->resize(100,200);
return app.exec();
}
因此,如果QTableWidget接受丢弃,则会在消息框中显示“Drop”消息。但是,不会来了=&gt;不接受放弃。在这个问题上有人可以帮助我吗?
注意:我在Windows平台上使用Qt-4.7.2
答案 0 :(得分:8)
您需要做两件事:
1)实施dragMoveEvent。这是在拖动进入QWidget时。实现可能如下所示:
void MyDropWidget::dragMoveEvent(QDragMoveEvent *event)
{
event->accept();
}
当您输入拖动哑剧数据时,无需在此处再次检查,但您可以检查您希望能够插入的区域。
2)不要调用父dragEnterEvent处理程序。所以你的dragEnterEvent函数将是这样的:
void MyDropWidget::dragEnterEvent(QDragEnterEvent * event)
{
if(event->mimeData()->hasFormat("text/plain"))
event->acceptProposedAction();
/* Don't need this.. QTableWidget::dragEnterEvent(event); */
}
原因是,QTableWidget支持它自己的拖放形式,所以即使你接受它,它也会将事件设置为被拒绝。