I am developing a TreeView with an Model on QStandardItemModel base and a QSortFilterProxyModel for filtering. It works fine. But when i want to use Drag&Drop i get always the parentIndex from Item i draged and also the parentIndex of item i droped. The row and col in QStandardItemModel::dropMimeData are always -1, so i should get the item itself, not the parent item.
If i leave the proxy away drag &drop works same. I always get in the model the parent item for the index. Where is my error?
Is there a good sample for me everywhere?
Here my source:
class QMyDragDropListViewModel : public QStandardItemModel
{
QMimeData* mimeData(const QModelIndexList &indexes) const
{
QMimeData *mimeData = new QMimeData();
QByteArray encodedData;
QDataStream stream(&encodedData, QIODevice::WriteOnly);
foreach (QModelIndex index, indexes) {
if (index.isValid()) {
QStandardItem*pItem = static_cast<QStandardItem*>( index.internalPointer() ); // this is the parentitem from item i realy drag
QString iID = pItem->text();
stream << iID;
}
}
mimeData->setData("application/Test/data", encodedData);
return mimeData;
}
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
QStandardItem *pItemParent = static_cast<QStandardItem*>( parent.internalPointer() ); // this is also the parent item from the item i realy dropped
}
QStandardItem * AddChild( const QString &text, const QVariant &data, QStandardItem *pParent, const QIcon& pIcon )
{
QStandardItem *pItem = new QStandardItem (pIcon,strName);
pItem->setData(data);
if (pParent)
{
pParent->setChild(pParent->rowCount(),pItem);
}
else
{
//invisibleRootItem()->setChild(invisibleRootItem()->rowCount(),pItem);
appendRow(pItem);
}
return pItem;
}
}
class QMySortFilterModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
QMySortFilterModel( QObject *pParent = NULL);
protected:
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
}
class QMyTreeview : public QTreeView
{
QMyTreeView()
{
QMyDragDropListViewModel model(this);
QMySortFilterModel proxy(this)
proxy.setSourceModel(&model);
setModel(&proxy);
}
}
Bookmarks