Results 1 to 2 of 2

Thread: QTreeView with QSortFilterProxyModel and Drag&Drop

  1. #1
    Join Date
    Nov 2014
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question QTreeView with QSortFilterProxyModel and Drag&Drop

    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);
    }
    }
    Last edited by CRohr; 7th November 2014 at 14:41.

  2. #2
    Join Date
    Nov 2014
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeView with QSortFilterProxyModel and Drag&Drop

    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;
    }

Similar Threads

  1. QTreeView Drag and Drop
    By theprobe in forum Qt Programming
    Replies: 1
    Last Post: 29th March 2011, 22:08
  2. QTreeView: Drag and Drop
    By laugusti in forum Newbie
    Replies: 7
    Last Post: 19th March 2010, 19:05
  3. Drag and drop in QTreeView
    By Valheru in forum Qt Programming
    Replies: 3
    Last Post: 27th July 2008, 09:36
  4. drag and drop from QTreeView
    By Untersander in forum Qt Programming
    Replies: 1
    Last Post: 10th April 2006, 09:00
  5. Drag & drop for QTreeView
    By yogeshm02 in forum Qt Programming
    Replies: 2
    Last Post: 30th January 2006, 14:32

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.