PDA

View Full Version : QTreeView with QSortFilterProxyModel and Drag&Drop



CRohr
7th November 2014, 11:47
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);
}
}

CRohr
7th November 2014, 14:47
I thing my problem is suited in the kind of adding the childs. But i am not sure.

Here my code to add a new item with some testings

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

QStandardItem* QMyDragDropListViewModel::AddChild( const QString &text const QVariant &data, QStandardItem *pParent, const QIcon& pIcon )
{
QStandardItem*pItem = new QStandardItem(pIcon,text);
pItem->setData(data);

if (pParent)
{
pParent->setChild(pParent->rowCount(),pItem);
#ifdef _DEBUG
QModelIndex index = indexFromItem(pItem);
QclDeviceItem *pItemTest = static_cast<QclDeviceItem*>( index.internalPointer() );
if(strName != pItemTest->text())
int iTest =0; // i always step into this line. The index does not fit to the item. It is always the index of the parent.
#endif
}
else
{
//invisibleRootItem()->setChild(invisibleRootItem()->rowCount(),pItem);
int iTest = rowCount();
appendRow(pItem);
QStandardItem *iTestItem = item(iTest);
if (strName != iTestItem->text())
int iTestFalse = 2;
else
{
#ifdef _DEBUG
QModelIndex index = indexFromItem(pItem);
QclDeviceItem *pItemTest = static_cast<QclDeviceItem*>( index.internalPointer() );
if(strName != pItemTest->text())
int iTest =0;// i always step into this line. The index does not fit to the item. It is always the index of the parent.
#endif
}
}
return pItem;
}