PDA

View Full Version : Dragging out of a QTreeView with a ProxyModel



jmichiel
17th June 2011, 22:37
I'm trying to get dragging to work using a QTreeView and a custom ProxyModel that contains a QSqlQueryModel. I'm using the ProxyModel to do grouping based on the values of some column of the data.
Now I want the user to be able to drag leafs of the tree out of the tree to another widget in my app.

I read http://doc.qt.nokia.com/4.7/model-view-programming.html#using-drag-and-drop-with-item-views and did everything stated there, but dragging (and dropping for that matter) doesn't work in the least: the drag is never started, startDrag() is never called (placed a breakpoint in QAbstractItemView code). The only thing that happens is that the item is selected.
What could I be doing wrong?
I don't need to subclass QTreeView since it should be able to handle it out-of-the-box, right?
The QTreeView is inside a QDockWidget, if that matters.

The drag and drop examples work, but they don't use a View, just plain Widgets.

Added after 50 minutes:

If I set the selectionBehaviour to SelectItems, it works...
Can it have something to do with the fact that I remove columns in my proxymodel, so that SelectRows has trouble selecting the entire row somehow?
Should I override mapSelectionFromSource/mapSelectionToSource to make it work on rows perhaps?

wysota
19th June 2011, 01:07
How did you implement flags() for your model? What is the value of the dragEnabled property for the view? How did you implement mimeData() for the model?

jmichiel
19th June 2011, 15:01
dragEnabled is true, and these are the flags and mimeData methods (pretty much as in the documentation:


Qt::ItemFlags ThemaLesModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags defaultFlags = sourceModel()->flags(mapToSource(index));
if (index.isValid())
return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
return defaultFlags;
}


QMimeData *ThemaLesModel::mimeData(const QModelIndexList &indexes) const
{
QMimeData *mimeData = new QMimeData();
QByteArray encodedData;

QDataStream stream(&encodedData, QIODevice::WriteOnly);

foreach (const QModelIndex &index, indexes) {
if (index.isValid()) {
QString text = data(index, Qt::DisplayRole).toString();
stream << text;
}
}

mimeData->setData("application/vnd.text.list", encodedData);
return mimeData;
}



If I use 'SelectRows', than I can't do anything. Using SelectItems, it works, but only for the original data rows, not for the 'virtual' ones I introduced myself for the grouping. While debugging, I saw that apparently it's the selectionModel that tells the view there are no selections when in SelectRows mode.

wysota
19th June 2011, 16:05
Is ThemaLesModel the proxy or the base model?

jmichiel
19th June 2011, 20:04
ThemaLesModel is derived from the Grouping proxy. The sourceModel is a QSqlQueryModel