Results 1 to 14 of 14

Thread: whole row drag/drop

  1. #1
    Join Date
    Sep 2007
    Posts
    99
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question whole row drag/drop

    hi guys...
    How can I set the model so it would take the whole row when dragging it ?
    I can set the selection behaviour to be select rows...but how can I set that the actual selected row would be dragged ?
    Or is it possible ? Or the only possibility is to use a selection model ?
    Thanks
    Last edited by gyre; 26th November 2007 at 20:13. Reason: updated contents

  2. #2
    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: whole row drag/drop

    Hmm, according to my test the whole row IS dragged out of the box when selection behavior is "select rows"...
    J-P Nurmi

  3. #3
    Join Date
    Sep 2007
    Posts
    99
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: whole row drag/drop

    Well yes...but I dont know how should I serialize them...

    Qt Code:
    1. QMimeData* QVcaMsgSeqModel::mimeData(const QModelIndexList &indexes) const
    2. {
    3. QMimeData *mimeData = new QMimeData();
    4. QByteArray encodedData;
    5.  
    6. QTextStream stream(&encodedData, QIODevice::WriteOnly);
    7.  
    8. foreach (QModelIndex index, indexes) {
    9. if (index.isValid()) {
    10. QString text = data(index, Qt::DisplayRole).toString();
    11. stream << text;
    12. }
    13. }
    14.  
    15. mimeData->setData("application/message", encodedData);
    16. return mimeData;
    17. }
    To copy to clipboard, switch view to plain text mode 

    I have 4 columns in each of my rows...now I have to serialize them so I could then decode them from the model...but if you consider that indexes in that "foreach cycle" doesnt neccessary mean that each column will be following each in one row than I dont know how to decode the data ...I got now into a dead point

  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: whole row drag/drop

    Did you try without reimplementing mimeData() and dropMimeData()? Just make itemData() return all the required roles in case you need to embed custom data.
    J-P Nurmi

  5. #5
    Join Date
    Sep 2007
    Posts
    99
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: whole row drag/drop

    no I havent...but shouldnt I reimplement those two methods to enable drag and drop ??

  6. #6
    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: whole row drag/drop

    Quote Originally Posted by gyre View Post
    no I havent...but shouldnt I reimplement those two methods to enable drag and drop ??
    Not necessarily, the default implementation in QAbstractItemModel is surprisingly good.
    J-P Nurmi

  7. #7
    Join Date
    Sep 2007
    Posts
    99
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: whole row drag/drop

    wel...but I have to reimplement drag/drop mouse events hm ?
    its not working somwhow without them...
    but maybe my codes are wrong
    Attached Files Attached Files

  8. #8
    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: whole row drag/drop

    Quote Originally Posted by gyre View Post
    wel...but I have to reimplement drag/drop mouse events hm ?
    No, you don't reimplement them either. Model's mimeData() and dropMimeData() will get called by the framework when appropriate. However, the default implementation of those is sufficient, you don't need to reimplement them either.

    First of all, make sure to drop all of these
    • QVcaMsgSeqModel::mimeData()
    • QVcaMsgSeqModel::dropMimeData()
    • QVcaMsgSeqModel::mimeTypes()


    Secondly, define a custom role to handle can messages. For example:
    Qt Code:
    1. enum
    2. {
    3. QVcaCanMsgRole = Qt::UserRole
    4. };
    To copy to clipboard, switch view to plain text mode 

    Then, reimplement itemData() as already advised:
    Qt Code:
    1. QMap<int, QVariant> QVcaMsgSeqModel::itemData(const QModelIndex &index) const
    2. {
    3. QMap<int, QVariant> roles = QAbstractTableModel::itemData(index);
    4. // add additional custom data
    5. roles.insert(QVcaCanMsgRole, QVariant::fromValue(msgList.at(index.row())));
    6. return roles;
    7. }
    To copy to clipboard, switch view to plain text mode 

    Now, make setData() handle the custom role and you're all set:
    Qt Code:
    1. bool QVcaMsgSeqModel::setData(const QModelIndex &index, const QVariant &value, int role)
    2. {
    3. ...
    4. if (index.isValid() && role == QVcaCanMsgRole)
    5. {
    6. msgList[row] = value.value<QVcaCanMsg>();
    7. }
    8. ...
    9. }
    To copy to clipboard, switch view to plain text mode 

    Notice that QVcaCanMsg must be declared to Qt's meta system to be able to use it with QVariant:
    Qt Code:
    1. Q_DECLARE_METATYPE(QVcaCanMsg);
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 26th November 2007 at 23:30. Reason: Fixed QVcaCanMsgRole to QVcaCanMsg where appropriate
    J-P Nurmi

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

    Default Re: whole row drag/drop

    Hmmm... what's the QVcaCanMsgRole meta type for? As far as I know you declare metatypes for classes and the role is... a number.

  10. #10
    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: whole row drag/drop

    Quote Originally Posted by wysota View Post
    Hmmm... what's the QVcaCanMsgRole meta type for? As far as I know you declare metatypes for classes and the role is... a number.
    Good notice! I meant QVcaCanMsg, of course.
    J-P Nurmi

  11. #11
    Join Date
    Sep 2007
    Posts
    99
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: whole row drag/drop

    Well this code works ...
    EXCEPT...every dragged row is dropped 3 times ( PLUS it is added at hte end of the TableView ....dunno why (...

    And another question...Is there any way how to FORBID drag and drop in actual widget...
    I mean...I dont want drop to be performed in inside the widget Im draggind items from...

    Thanks...
    Attached Files Attached Files

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

    Default Re: whole row drag/drop

    You can always check if the source of the drag is the same as the current widget. As for forbidding all drops, there is "acceptDrops" property in QWidget.

  13. #13
    Join Date
    Sep 2007
    Posts
    99
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: whole row drag/drop

    so I made it work...except for one little thing...that the items are not droped at the actual position but still at the end ...
    Attached Files Attached Files

  14. #14
    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: whole row drag/drop

    May I ask, why do you insist of rewriting error prone mimeData() and dropMimeData() if QAbstractItemModel/QAbstractTableModel already implements them for you? Just take a look at src/corelib/kernel/qabstractitemmodel.cpp if you don't believe me. All the data returned by itemData() is packed to MIME data by default so you can simply embed your CAN messages to those. As a proof of concept, I have attached an example which does not reimplement mimeData() nor dropMimeData().
    Attached Files Attached Files
    J-P Nurmi

Similar Threads

  1. QTreeWidget drag/drop
    By s_a_white in forum Newbie
    Replies: 1
    Last Post: 10th February 2007, 22:04

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.