Hi,
I have a problem concerning drag'n'drop and QTableWidget. I have subclassed QTableWidget and implemented drag'n'drop by reimplementing dragEnterEvent, dragMoveEvent and dropEvent. It works as I expect. But if I place a QLabel as a child on the QTableWidget, I cannot drop items on the QLabel ( I want to receive the dropevent in QTableWidget). Here is some sample code
table.h
#ifndef TABLE_H
#define TABLE_H
#include <QTableWidget>
{
Q_OBJECT
public:
explicit Table
(QWidget *parent
= 0);
protected:
};
#endif // TABLE_H
#ifndef TABLE_H
#define TABLE_H
#include <QTableWidget>
class Table : public QTableWidget
{
Q_OBJECT
public:
explicit Table(QWidget *parent = 0);
protected:
void dragEnterEvent(QDragEnterEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void dropEvent(QDropEvent *event);
};
#endif // TABLE_H
To copy to clipboard, switch view to plain text mode
table.cpp
#include "table.h"
#include <QLabel>
#include <QDebug>
#include <QDragEnterEvent>
#include <QDragMoveEvent>
{
setMinimumSize(200, 200);
setAcceptDrops(true);
label->setText("a long dummy text\na long dummy text\na long dummy text\na long dummy text\n");
label->move(30, 32);
label->setEnabled(true);
// label->setAcceptDrops(false)
}
{
qDebug() << "Table:dragEnterEvent";
event->acceptProposedAction();
}
{
qDebug() << "Table:dragMoveEvent";
event->acceptProposedAction();
}
{
qDebug() << "Table:dropEvent";
}
#include "table.h"
#include <QLabel>
#include <QDebug>
#include <QDragEnterEvent>
#include <QDragMoveEvent>
Table::Table(QWidget *parent) :
QTableWidget(parent)
{
setMinimumSize(200, 200);
setAcceptDrops(true);
QLabel* label = new QLabel(this);
label->setText("a long dummy text\na long dummy text\na long dummy text\na long dummy text\n");
label->move(30, 32);
label->setEnabled(true);
// label->setAcceptDrops(false)
}
void Table::dragEnterEvent(QDragEnterEvent* event)
{
qDebug() << "Table:dragEnterEvent";
event->acceptProposedAction();
}
void Table::dragMoveEvent(QDragMoveEvent* event)
{
qDebug() << "Table:dragMoveEvent";
event->acceptProposedAction();
}
void Table::dropEvent(QDropEvent* event)
{
qDebug() << "Table:dropEvent";
}
To copy to clipboard, switch view to plain text mode
I tried different combinations of label->setEnabled(true/false) and label->setAcceptDrops(true, false). I also tried to subclass QLabel and explicitly propagate dragEnterEvent, dragMoveEvent and dropEvent to QTableWidget by calling event->ignore().
Interestingly, the above code works if I replace QTableWidget by QWidget.
Any ideas of a clean Qt solution?
Qt 4.6 tested on Linux and Windows.
Thanks!
Bookmarks