Results 1 to 20 of 21

Thread: Can I get a drag&drop tobe displayed as a line instead of the big floating icon?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Can I get a drag&drop tobe displayed as a line instead of the big floating icon?

    Drag and drop is mostly handled by the model and not by the view.


    These methods (of the model) may be of interest for you:
    bool dropMimeData ( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent )
    QMimeData * mimeData ( const QModelIndexList & indexes ) const
    QStringList mimeTypes () const
    Qt::DropActions supportedDropActions () const

  2. The following user says thank you to wysota for this useful post:

    pir (21st May 2006)

  3. #2
    Join Date
    May 2006
    Location
    Stockholm, Sweden
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11
    Thanks
    12
    Thanked 1 Time in 1 Post

    Default Re: Can I get a drag&drop tobe displayed as a line instead of the big floating icon?

    ok... stuck again.

    I have problem with passing the information that is dragged... I've tried QAbstractModel::mimeData(), do I need to reimplement it or is it just ok to pass the QModelIndex into it?

    Then I have a big problem with retrieving the QModelIndex back from the QMimeData.... how am I supposed to do that? There is a function called QMimeData::retrieveData(), can I use that one as it is or do I reimplement it? It is protected, which I find kind of weird if it supposed to be used in this way...

    ahhhh, I'm tired of this drag n dropping stuff...

    /pir

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

    Default Re: Can I get a drag&drop tobe displayed as a line instead of the big floating icon?

    Quote Originally Posted by pir
    ok... stuck again.

    I have problem with passing the information that is dragged... I've tried QAbstractModel::mimeData(), do I need to reimplement it or is it just ok to pass the QModelIndex into it?

    Then I have a big problem with retrieving the QModelIndex back from the QMimeData.... how am I supposed to do that? There is a function called QMimeData::retrieveData(), can I use that one as it is or do I reimplement it? It is protected, which I find kind of weird if it supposed to be used in this way...
    You should not pass indexes there. You need to encode your data as some custom mime-type (for example application/x-my-mime-type). If you only drag within the same widget, you can for example use the index row number as the encoded form of data (as long as you're able to recreate an item from it). The easiest way is something like this:

    Qt Code:
    1. QMimeData * MyModel::mimeData ( const QModelIndexList & indexes ) const{
    2. QMimeData *mime = new QMimeData;
    3. QString rows;
    4. foreach(QModelIndex index, indexes){
    5. rows += QString::number(index.row())+"; ";
    6. }
    7. mime->setText(rows);
    8. return mime;
    9. }
    To copy to clipboard, switch view to plain text mode 

    In dropMimeData just reconstruct indices from their row values and you're done. Of course you may choose to do it more properly and use QByteArray and a custom mime-type instead of text/plain (which is used when using setText()). Or you could even do it even more properly and encode not the index of the item but its data (as should be done with "real" drag&drop).

  5. #4
    Join Date
    May 2006
    Location
    Stockholm, Sweden
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11
    Thanks
    12
    Thanked 1 Time in 1 Post

    Default Re: Can I get a drag&drop tobe displayed as a line instead of the big floating icon?

    Quote Originally Posted by wysota
    .../ Or you could even do it even more properly and encode not the index of the item but its data (as should be done with "real" drag&drop).
    This fit my purposes best, I think. The only thing my indexes containt is a pointer to a component in my scene. So it should be sufficuent to give this pointer as information... this was actually my first approach but I couldn't find how I was supposted to convert the pointer to a QByteArray and back.

    But there is one problem with this approach. I need to be able to remove the old index when the user have dropped the object, since it should be executed as a move action. So I need to find that index, and then I suppose I need to know its row and parent, and the parent's row and parent etc... maybe it's simpler to have a pointer in my model class pointing to the index being dragged and use this information instead of a mime. Could this create problems somehow?

    /pir
    Last edited by pir; 22nd May 2006 at 07:27.

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

    Default Re: Can I get a drag&drop tobe displayed as a line instead of the big floating icon?

    Quote Originally Posted by pir
    This fit my purposes best, I think. The only thing my indexes containt is a pointer to a component in my scene. So it should be sufficuent to give this pointer as information... this was actually my first approach but I couldn't find how I was supposted to convert the pointer to a QByteArray and back.
    A pointer is an integer, just treat it as such. But if you want to encode the data itself, then I didn't mean a pointer here, but actual information which is stored in an object referenced by the pointer.


    But there is one problem with this approach. I need to be able to remove the old index when the user have dropped the object, since it should be executed as a move action.
    I guess the view might do it yourself if you convince it you are doing a move action (but I'm not sure, haven't tried it myself yet).

    So I need to find that index, and then I suppose I need to know its row and parent, and the parent's row and parent etc...
    That's why the concept of indexes exists But if you implement the drop correctly, you won't have to worry about removing the item from its original place.

    maybe it's simpler to have a pointer in my model class pointing to the index being dragged and use this information instead of a mime. Could this create problems somehow?
    This doesn't give you any advantage. You can store the pointer in the QMimeData object as well.

  7. The following user says thank you to wysota for this useful post:

    pir (22nd May 2006)

  8. #6
    Join Date
    Aug 2006
    Posts
    163
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    12
    Thanked 5 Times in 4 Posts

    Default Re: Can I get a drag&drop tobe displayed as a line instead of the big floating icon?

    What may help you a bit is to look at my source code here : http://code.google.com/p/knewz/source/browse/trunk
    I ran into this the other day, it's a bit of work to get right but not terribly complicated once you get the hang of it. There's a few things to remember while dragging and dropping stuff, but you should be able to pick them up from my code if you haven't already.
    There pertinant stuff you will want to look at is the model and the mimedata implementations:
    http://code.google.com/p/knewz/sourc...knewzmodel.cpp
    http://code.google.com/p/knewz/sourc...zbmimedata.cpp

    Like you, I am just passing pointers to objects around in a QList. So with a drag and drop you just dump the pointer list into a custom mimedata class and retrieve it when it is dropped. Then you can use your underlying data structure to manipulate the data directly, encapsulating any removal and insertion of rows with the correct calls to being/endRemoveRows and begin/endInsertRows

Similar Threads

  1. QTcpSocket exception.
    By Fastman in forum Qt Programming
    Replies: 9
    Last Post: 29th January 2008, 13:51
  2. Some very weird compilation warnings
    By MarkoSan in forum Qt Programming
    Replies: 21
    Last Post: 23rd January 2008, 16:48
  3. Qwizard crashed when created in a slot
    By joshlareau in forum Qt Programming
    Replies: 9
    Last Post: 15th January 2008, 09:16
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  5. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42

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
  •  
Qt is a trademark of The Qt Company.