Results 1 to 5 of 5

Thread: QListView trouble accepting drops

  1. #1
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QListView trouble accepting drops

    I was trying to make a simple example to have a QListView accept a dropped file from the OS and can't seem to get the thing to work. I was reading about it in the Qt Assistant article "Using Drag and Drop with Item Views" and I understand that the view and the model need to be able to accept drops. I'm pretty sure that the view properties are set correctly, but I'm not sure about the model. What am I missing?

    Here's the complete code:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class AdvQDirModel: public QDirModel
    4. {
    5. public:
    6. AdvQDirModel(): QDirModel(){ }
    7. ~AdvQDirModel() { }
    8.  
    9. Qt::DropActions supportedDropActions() const
    10. { return Qt::CopyAction | Qt::MoveAction; }
    11.  
    12. };
    13.  
    14. int main(int argc, char *argv[])
    15. {
    16. QApplication app(argc, argv);
    17.  
    18. AdvQDirModel model;
    19. QListView listView;
    20. listView.setAcceptDrops(true);
    21. listView.setDropIndicatorShown(true);
    22. listView.setModel(&model);
    23.  
    24. listView.setWindowTitle(QObject::tr("Dir View"));
    25. listView.resize(320, 240);
    26. listView.show();
    27.  
    28. return app.exec();
    29. }
    To copy to clipboard, switch view to plain text mode 

    Thanks,
    Paul

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListView trouble accepting drops

    QDirModel has a reimplemented version of supportedDropActions from QAbstractItemModel..
    If your model is derived from it or is a subclass of QAbstractItemModel, then you might
    want to reimplement it too.

    Also, take a look at QAbstractItemModel::dropMimeData.

    Regards

  3. #3
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QListView trouble accepting drops

    As my example shows, I'm reimplementing supportedDropActions, so I don't think thats the problem.

    I had started to write a simple dropMimeData for the new model -> AdvQDirModel, but when I set a breakpoint in my dropMimeData, run the app and drag a file from my desktop to it, it never enters the function. So I don't think that isn't the problem either.

    Paul

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QListView trouble accepting drops

    Set QDirModel::setReadOnly(false) and reimplement QDirModel::flags() to return Qt::ItemIsDropEnabled:
    Qt Code:
    1. class AdvQDirModel: public QDirModel
    2. {
    3. public:
    4. AdvQDirModel(): QDirModel() { setReadOnly(false); } // <---
    5. ~AdvQDirModel() { }
    6.  
    7. Qt::ItemFlags flags(const QModelIndex& index) const
    8. {
    9. Qt::ItemFlags f = QDirModel::flags(index);
    10. if (isDir(index))
    11. f |= Qt::ItemIsDropEnabled; // <---
    12. return f;
    13. }
    14. };
    To copy to clipboard, switch view to plain text mode 

    EDIT: Actually, now that I relook at it, all you need is to set QDirModel::setReadOnly(false) and that's all. You don't even need to reimplement flags(). The default implementation will enable dropping once read only property is set to false and the dropping target is a writable directory.
    Qt Code:
    1. QDirModel model;
    2. model.setReadOnly(false);
    3. QListView listView;
    4. ...
    5. listView.setModel(&model);
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 6th September 2007 at 19:50.
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    thomaspu (6th September 2007)

  6. #5
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QListView trouble accepting drops

    Ah, thanks Jpn. The flags is what I needed to get it working.

    Paul

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.