Results 1 to 2 of 2

Thread: QTreeView with QSortFilterProxyModel and Drag&Drop

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 15:41.

Similar Threads

  1. QTreeView Drag and Drop
    By theprobe in forum Qt Programming
    Replies: 1
    Last Post: 29th March 2011, 23:08
  2. QTreeView: Drag and Drop
    By laugusti in forum Newbie
    Replies: 7
    Last Post: 19th March 2010, 20:05
  3. Drag and drop in QTreeView
    By Valheru in forum Qt Programming
    Replies: 3
    Last Post: 27th July 2008, 10:36
  4. drag and drop from QTreeView
    By Untersander in forum Qt Programming
    Replies: 1
    Last Post: 10th April 2006, 10:00
  5. Drag & drop for QTreeView
    By yogeshm02 in forum Qt Programming
    Replies: 2
    Last Post: 30th January 2006, 15: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.