Results 1 to 15 of 15

Thread: Dropping onto a QSqlQueryModel

  1. #1
    Join Date
    Jan 2006
    Posts
    75
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Dropping onto a QSqlQueryModel

    I want to be able to drop an object onto a QSqlQueryModel, which is contained in a QListView. Now, in order for the drop action to work, do I need to reimplement dropMimeData?

  2. #2
    Join Date
    May 2006
    Posts
    28
    Thanks
    8
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Dropping onto a QSqlQueryModel

    Hi,
    I have been doing some drag and drop within a treeView here:
    http://www.qtcentre.org/forum/f-newb...ited-2829.html

    Your drop will work, without dropMimeData but, you may need it to make the drop work as you wanted.

    You could do functionality in : MyTreeView::dropEvent(QDropEvent *event)
    but it is probably not what you want.

    I have not used dropMimeData yet but to make the model use the QSqlQueryModel it is probably a good idea.

    Hope this helps,

  3. #3
    Join Date
    Jan 2006
    Posts
    75
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dropping onto a QSqlQueryModel

    I have reimplemented dropEvent() in my new class, but for some reason the action of dropping doesn't call dropEvent(). I'm not sure what I'm doing wrong quite yet :P

  4. #4
    Join Date
    May 2006
    Posts
    28
    Thanks
    8
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Dropping onto a QSqlQueryModel

    Mybe I could help if you post some code.

  5. #5
    Join Date
    Jan 2006
    Posts
    75
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dropping onto a QSqlQueryModel

    Qt Code:
    1. MyListView::MyListView(QWidget *parent)
    2. :QListView(parent)
    3. {
    4. setAcceptDrops(true);
    5. }
    6.  
    7. void MyListView::dragEnterEvent(QDragEnterEvent * event)
    8. {
    9. if(event->mimeData()->hasFormat("text/uri-list"))
    10. {
    11. std::cout << "OK" << std::endl;
    12. event->accept();
    13.  
    14. }
    15. std::cout << "drag" << std::endl;
    16. }
    17.  
    18. void MyListView::dropEvent(QDropEvent * event)
    19. {
    20. std::cout << "Drop" << std::endl;
    21. event->acceptProposedAction();
    22. }
    To copy to clipboard, switch view to plain text mode 

    Now when I drag something into the mylistview widget, dragEnterEvent fires and the mimeData format is correctly interpreted, but dropping doesn't do anything.

    In fact when I drag something over the widget, the cursor doesn't turn into a drop cursor. It turns into what I would describe as an action denied cursor.

    Please note that I haven't written any code for the model that this listview uses. I'm using a generic QSqlQueryModel.

  6. #6
    Join Date
    May 2006
    Posts
    28
    Thanks
    8
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Dropping onto a QSqlQueryModel

    Yeah thats the problem I think,
    You will have to subclass the Model and implement :

    Qt Code:
    1. QStringList MyModel::mimeTypes () const
    2. {
    3. list << ""text/uri-list"";
    4. return list;
    5. }
    To copy to clipboard, switch view to plain text mode 

    This should stop the action denied cursor from appearing

  7. The following 2 users say thank you to Big Duck for this useful post:

    kroenecker (6th July 2006), montuno (12th May 2009)

  8. #7
    Join Date
    Jan 2006
    Posts
    75
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dropping onto a QSqlQueryModel

    Big Duck you rock. How in the world did you figure that out? I need to look over that particular virtual function I guess.

  9. #8
    Join Date
    Jan 2006
    Posts
    75
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dropping onto a QSqlQueryModel

    Now I am trying to figure out my next step. Again, I want to drag an item into the field (done and works). Then I want to check the items type (this is easy because dropped items return a mimetype of text/uri-list in windows).

    My question is: after dropping something, how do I let the model know that I want it to act on the data? I cannot just perform actions in MyListView::dropEvent() because, to complicate matters, I want to be able to check:

    1) that if I drop an object that is already present in the itemlist, it is ignored (for now).
    2) if it isn't in the list then it is copied to a specified location and that location is then added to my database as an absolute path.

    Admittedly, I'm a bit slow on the uptake as to how this all works, but I really appreciate your suggestions.

  10. #9
    Join Date
    Jan 2006
    Posts
    75
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dropping onto a QSqlQueryModel

    I almost feel like I need to use createIndex() somehow because dropMimeData seems to require that I drop on an already existsing item. Am I reading that correctly? Anyway here is some more code:

    Qt Code:
    1. bool MyModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
    2. int row, int column, const QModelIndex &parent)
    3. {
    4. std::cout << "MyModel" << std::endl;
    5. if(action == Qt::IgnoreAction)
    6. return true;
    7.  
    8. if(!data->hasFormat("text/uri-list"))
    9. return false;
    10.  
    11. if(column > 0)
    12. return false;
    13.  
    14. if(action == Qt::CopyAction)
    15. {
    16. std::cout << "copyaction" << std::endl;
    17. return true;
    18. }
    19. return false;
    20. }
    21.  
    22. QStringList MyModel::mimeTypes() const
    23. {
    24. list << "text/uri-list";
    25. return list;
    26. }
    To copy to clipboard, switch view to plain text mode 

    To say the least dropMimeData(...) isn't being called when I drop something into the model.

  11. #10
    Join Date
    Jan 2006
    Posts
    75
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dropping onto a QSqlQueryModel

    Do I need to reimplement insertRow() possibly?

  12. #11
    Join Date
    May 2006
    Posts
    28
    Thanks
    8
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Dropping onto a QSqlQueryModel

    I havent used dropMimeData data so I cant help more, but would like to know how you get it to work when you do

    You could try some of the following:

    Qt:: DropActions supportedDropActions () const;
    virtual Qt::ItemFlags flags ( const QModelIndex & index ) const;
    virtual QMimeData * mimeData ( const QModelIndexList & indexes ) const

    These docs may help too:
    http://doc.trolltech.com/4.1/model-view-dnd.html
    http://doc.trolltech.com/4.1/model-v...-type-handling

  13. #12
    Join Date
    Jan 2006
    Posts
    75
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dropping onto a QSqlQueryModel

    No problem B D(awg). You have been a big help. Hopefully the weekend will offer enough free time for me to figure this out.

  14. #13
    Join Date
    Jan 2006
    Posts
    75
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dropping onto a QSqlQueryModel

    I'm finding this to be really difficult. If someone has an example that I could use, I'd appreciate it.

  15. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Dropping onto a QSqlQueryModel

    If you want to use dropMimeData (which you should), you can't reimplement events connected with dropping. You shouldn't reimplement them at all, because this way you act on the view and not on the model.

    You should reimplement three methods from the model -- dropMimeData, mimeTypes and possibly mimeData (the latter only in case you want to drag items from the model and not only drop on them). mimeTypes should return a list of mime types which describe items in the model. If you don't want to drag items from the model it is possible you don't need to do anything here, although I'm not sure about it. dropMimeData should be reimplemented to take an action when an actual drop occurs. Remember that new items are dropped onto existing ones and corresponding indexes should already be created by the model there, so you should only fill the data of the item.

    Remember to reimplement flags() too to make sure items are DropEnabled.

    Your problem may lie elsewhere -- QSqlQueryModel is a read-only model, so you may not add any data to it. You either have to use another model (like QSqlTableModel) or make some smart reimplementation (the word smart is crucial here) to make QSqlQueryModel write enabled.

  16. #15
    Join Date
    Jan 2006
    Posts
    75
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dropping onto a QSqlQueryModel

    This is a sort of "note to self" post:

    After upgrading to 4.2 I had problems with drag and drop again so I reimplemented the flags returned as below:

    Qt Code:
    1. Qt::ItemFlags MyModel::flags(const QModelIndex &index) const
    2. {
    3. Qt::ItemFlags defaultFlags = QSqlQueryModel::flags(index);
    4. return Qt::ItemIsDropEnabled | defaultFlags;
    5. }
    To copy to clipboard, switch view to plain text mode 

    This fixed everything.

    Note: MyModel derives from SqlQueryModel, which can't be edited by design, so I had to return ItemIsDropEnabled in order to get my code to work again.
    Last edited by wysota; 16th November 2006 at 21:29. Reason: missing [code] tags

Similar Threads

  1. QSqlQueryModel + set Write
    By raphaelf in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2006, 08:55

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.