Thanks again for your repply, here goes the code:
This goes in a custom QTreeView class:
@
void CTreeView::mousePressEvent ( QMouseEvent *event ) {
if (event->button() == Qt::LeftButton)
mDragStartPos = event->pos();
// event->accept();
// QTreeView::mousePressEvent(event);
}
void CTreeView::mouseMoveEvent ( QMouseEvent * event ) {
if (event->buttons() == Qt::LeftButton &&
(mDragStartPos - event->pos()).manhattanLength() >=
QApplication::startDragDistance()) {
clearSelection();
QDrag * drag = new QDrag(this);
QMimeData * mimeData = new QMimeData;
QModelIndex index = indexAt(mDragStartPos);
CTreeItem * item = static_cast<CTreeItem*>(index.internalPointer());
if (item) {
qint8 toolType = item->data(5).toInt();
QString toolCode = item->data(8).toString();
QString toolIcon = item->data(4).toString();
QByteArray mdata;
QDataStream stream(&mdata, QIODevice::WriteOnly);
QMap<int, QVariant> roleDataMap;
roleDataMap[0] = QVariant(toolType);
roleDataMap[1] = QVariant(toolCode);
stream << roleDataMap;
mimeData->setData(QString("application/x-qabstractitemmodeldatalist"),
mdata);
drag->setPixmap(toolIcon);
drag->setMimeData(mimeData);
drag->exec(Qt::CopyAction | Qt::MoveAction | Qt::IgnoreAction,
Qt::MoveAction);
}
}
// event->accept();
// QTreeView::mouseMoveEvent(event);
}
@
As you can see, I have tried to keep the QTreeView event processing for both cases. Also tried to ignore and to accept the events, with no noticeable effect.
This goes in a custom QAbstractItemModel, which is set as the model/view class of the previous one:
@
Qt::DropActions CTreeModel::supportedDropActions () const {
return Qt::CopyAction | Qt::MoveAction;
}
bool CTreeModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
int row, int column, const QModelIndex &parent) {
if ( data->hasFormat("application/x-qabstractitemmodeldatalist") &&
( (action == Qt::MoveAction) || (action == Qt::CopyAction) ) ) {
QByteArray encoded = data->data("application/x-qabstractitemmodeldatalist");
QDataStream stream(&encoded, QIODevice::ReadOnly);
QMap<int, QVariant> roleDataMap;
while (!stream.atEnd()) {
stream >> roleDataMap;
}
mDroppedCode = roleDataMap[1].toString();
if (parent.isValid()) {
QModelIndex index = this->index(row, column, parent);
CTreeItem * it = item(index);
int i = it->childCount();
i++;
}
return true;
} else {
return false;
}
}
@
Thanks again.
Francisco