Qt - QTableWidget 不接受 drop
我正在制作一个使用 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”。但是,不来=>不接受掉落。有人可以帮助我解决这个问题吗?
注:我在windows平台下使用Qt-4.7.2
I am making a project which uses QTableWidget. When I tried to make that to accept the drops, I came to know that it is not behaving as I thought. The thing is, If I change QTableWidget into QWidget then drop got accepted. So, the problem is not in coding. Here comes the code, and as for as I am concerning the problem is in "MyDropWidget" class
#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();
}
So, if the QTableWidget accepts the drop, then a message box will come up with a message "Drop". But, is not coming=> drop is not accepted. Can anybody help me in this issue?
Note: I use Qt-4.7.2 in windows platform
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要做两件事:
1)实现dragMoveEvent。这是当拖动进入 QWidget 时的情况。实现可能如下所示:
当您在输入时检查拖动 mime 数据时,无需在此处再次检查,但您可以检查您希望能够放入的区域。
2) 不要调用父 dragEnterEvent 处理程序。因此,您的 DragEnterEvent 函数将如下所示:
原因是,QTableWidget 支持它自己的拖放形式,因此即使您接受事件,它也会将事件设置为被拒绝。
There are two things you need to do:
1) Implement dragMoveEvent. This is for when the drag has entered the QWidget. An implementation might look like this :
As you checked the drag mime data when it entered, no need to check it again here, but you could do checking of the area you want to be able to drop in to.
2) Don't call the parent dragEnterEvent handler. So your dragEnterEvent function would be like this:
Reason is, QTableWidget supports it's own form of drag-drop so it's setting the event back to being rejected even though you are accepting it.