PDA

View Full Version : Dropping on QTableWidget's child



blacker1611
1st July 2010, 09:04
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>

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

table.cpp

#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";
}

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!

blacker1611
2nd July 2010, 08:48
Nobody has an idea? Can this be a bug by Qt?

blacker1611
7th July 2010, 12:25
I use an workaround now: I've subclassed QLabel and implemented the same drag'drop functionality for the Label. Since this contains a lot of redundant code I would still be glad if someone could tell me if I'm doing something wrong or if this is a bug by Qt!