PDA

View Full Version : QTreeWidgetItem mime type



themolecule
27th August 2007, 04:27
I want to set the mime information for a QTreeWidgetItem so that drag and drop will properly work. Do I have to subclass treewidget or treewidgetitem or can I just set something somewhere in the tree items before they are dragged (like when they are created)?

marcel
27th August 2007, 05:26
See the examples here: http://doc.trolltech.com/4.3/dnd.html#dragging.

Regards

jpn
27th August 2007, 09:42
You might want to take a look at QTreeWidget::mimeData() and QTreeWidget::dropMimeData().

themolecule
27th August 2007, 13:04
I see how it is possible to subclass... this is well documented.

I really just want to drop the first (and only) column from the treewidgetitem onto the target (perhaps as mime text/plaintext)... I thought there may be a simple way or it was already implemented and I was missing it.

If I create a QTreeWidget and populate it with QTreeWidgetItems (not subclassing either one) then when I drag from the treewidget to somethere else the mime seems to be empty. Perhaps the text is hidden somewhere in the mimeData? It seems odd that there is nothing there, because the documentation seems to imply that D&D within the tree widget would work without subclassing if the right flags are turned on, which implies that TreeWidgetItem mime data has some default implementation.

Perhaps I have to populate the treewidgetitem with data of a different role? The one I am using is Qt::DisplayRole.

joshlareau
19th October 2007, 16:15
"By default, the built-in models and views use an internal MIME type (application/x-qabstractitemmodeldatalist) to pass around information about model indexes"

Have you tried something like the following:



void dropEvent( QGraphicsSceneDragDropEvent* event )
{
if (event->mimeData()->hasFormat("application/x-qabstractitemmodeldatalist"))
{
QTreeWidget *tree = dynamic_cast<QTreeWidget *>(event->source());

QByteArray itemData = event->mimeData()->data("application/x-qabstractitemmodeldatalist");
QDataStream stream(&itemData, QIODevice::ReadOnly);

int r, c;
QMap<int, QVariant> v;
stream >> r >> c >> v;

QTreeWidgetItem *item = tree->topLevelItem(r);

if( item )
{
itemDropped(item);
}
}

}

maximAL
27th November 2007, 12:52
does the standard mime-type actually contain information on sub-items etc.?
it seems when using drag 'n drop with trees, the structure of the items gets flattened to a list.

jpn
27th November 2007, 12:55
it seems when using drag 'n drop with trees, the structure of the items gets flattened to a list.
Yes, that's unfortunate. I'm afraid you will have to reimplement QTreeWidget::mimeData() and QTreeWidget::dropMimeData() and store items into MIME data in hierarchical format to achieve that.