Greetings all,
Regular lurker, first time posting. I want to be able to allow drops only on certain items in a QTreeWidget. Is it possible to subclass QTreeWidgetItem to allow it to be a "drop target" for a drag & drop operation? The code below shows the approach I tried, but it doesn't work correctly: only the first MyTreeWidgetItem I add to the tree control accepts drops! The fact that this only 'sort of' works makes me suspect I'm bumping into a side effect of some of Qt's C++ magic. Is it possible to subclass QTreeWidgetItem to accept drops? Alternatively, if there is another way to achieve the goal of fine-grained control of which items can be "dropped on", I'd be happy to learn about that.
Thanks!
Paul Kerchen
Platform/environment: Qt 4.3.3, MSVC 7.1, Windows XP
{
Q_OBJECT
public:
signals:
void itemDropped
( MyTreeWidgetItem
* item,
QWidget* source
);
protected:
};
MyTreeWidgetItem::MyTreeWidgetItem( TreeWidget* parent )
{
setAcceptDrops(true);
}
void MyTreeWidgetItem::
{
event->setDropAction(Qt::CopyAction);
event->acceptProposedAction();
}
void MyTreeWidgetItem::
{
event->setDropAction(Qt::CopyAction);
event->acceptProposedAction();
}
void MyTreeWidgetItem::
{
event->setDropAction(Qt::CopyAction);
event->acceptProposedAction();
emit itemDropped(this, event->source());
}
class MyTreeWidgetItem : public QWidget, public QTreeWidgetItem
{
Q_OBJECT
public:
MyTreeWidgetItem( QTreeWidget* parent);
signals:
void itemDropped( MyTreeWidgetItem* item, QWidget* source );
protected:
void dragEnterEvent(QDragEnterEvent* event);
void dragMoveEvent(QDragMoveEvent* event);
void dropEvent(QDropEvent* event);
};
MyTreeWidgetItem::MyTreeWidgetItem( TreeWidget* parent )
: QWidget(parent)
, QTreeWidgetItem( parent )
{
setAcceptDrops(true);
}
void MyTreeWidgetItem::
dragEnterEvent(QDragEnterEvent* event)
{
event->setDropAction(Qt::CopyAction);
event->acceptProposedAction();
}
void MyTreeWidgetItem::
dragMoveEvent(QDragMoveEvent* event)
{
event->setDropAction(Qt::CopyAction);
event->acceptProposedAction();
}
void MyTreeWidgetItem::
dropEvent(QDropEvent* event)
{
event->setDropAction(Qt::CopyAction);
event->acceptProposedAction();
emit itemDropped(this, event->source());
}
To copy to clipboard, switch view to plain text mode
Bookmarks