Page 2 of 2 FirstFirst 12
Results 21 to 37 of 37

Thread: Drag and Drop, dropEvent not being called?

  1. #21
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: Drag and Drop, dropEvent not being called?

    Here is a working example.
    Attached Files Attached Files

  2. #22
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag and Drop, dropEvent not being called?

    Many thanks, that is very kind of you.

  3. #23
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag and Drop, dropEvent not being called?

    Many thanks for the example, I think way back in the thread or a related one you did mention that you should only implement the dropMimeData, mimeData and mimeTypes functions, I should have listened!!! I had implemented a few mouse event functions and a few drag events in my tree widget which I shouldn't have done, this was stopping it working.

    Is it possible to pass over the object associated with the item being dragged from the tree widget?

    Kind regards,
    Steve

  4. #24
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: Drag and Drop, dropEvent not being called?

    Quote Originally Posted by steg90 View Post
    Many thanks for the example, I think way back in the thread or a related one you did mention that you should only implement the dropMimeData, mimeData and mimeTypes functions, I should have listened!!! I had implemented a few mouse event functions and a few drag events in my tree widget which I shouldn't have done, this was stopping it working.
    I think we told you a few times to remove the mouse event handlers

    Is it possible to pass over the object associated with the item being dragged from the tree widget?
    In general, yes. You can pass a pointer or something like that, you can subclass QMimeData if you want. But the proper way to go would be to serialize your object to QByteArray, transfer that array using QMimeData::setData and reconstruct the object on the other side.

  5. #25
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag and Drop, dropEvent not being called?

    Hi again,

    This is my mimeData function within the treewidget :

    Qt Code:
    1. QMimeData * DASignalTree::mimeData ( const QList<QTreeWidgetItem *> items ) const
    2. {
    3. if(items.isEmpty()) return 0;
    4. QTreeWidgetItem* parent = items[0]->parent();
    5. CDADcb::CSignal* pSignal = items[0]->data( 0, Qt::UserRole ).value<CDADcb::CSignal*>();
    6. CDADcb::CMessage* pMess = parent->data( 0, Qt::UserRole ).value<CDADcb::CMessage*>();
    7. theApp->m_dcb.m_oSignalMap.insert( pMess->m_strId, (QObject*)pSignal );
    8.  
    9. QMimeData *md = new QMimeData;
    10. md->setData("application/x-example", items[0]->data(0, Qt::DisplayRole).toByteArray());
    11. return md;
    12. }
    To copy to clipboard, switch view to plain text mode 

    The maps are what the model uses for its data. Problem with the above is, the model code now crashes in the data method ( the model data is updated in a thread every 5 milliseconds ).

    Your solution sounds better, how would I serialize an object of the following and I'm presuming it would be reconstructuted in the models dropMimedata method?

    Many thanks for your invaluable help!
    Steve

  6. #26
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: Drag and Drop, dropEvent not being called?

    The easiest way is to register your types within QVariant using Q_DECLARE_METATYPE and qRegisterMetaTypeStreamOperators which in theory should allow you to just redirect your map to a byte array through QDataStream.

    I certainly wouldn't use pointers if your data changes rapidly. Your code seems a bit complicated and I'm wondering why you even reveal the pointers to the outside world in your model... Don't these "signals" have some kind of textual representation you could use?

  7. #27
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag and Drop, dropEvent not being called?

    Hi,

    I did do the register of my objects with Q_DECLARE_METATYPE as they are used in other places. The model uses a hash map for its 'internal' structure which gets updated on the fly. I have the following code now which works fine for the drag and drop operation :

    Qt Code:
    1. QMimeData * DASignalTree::mimeData ( const QList<QTreeWidgetItem *> items ) const
    2. {
    3. if(items.isEmpty()) return 0;
    4.  
    5. QTreeWidgetItem* parent = items[0]->parent(); // message
    6.  
    7. if( parent )
    8. {
    9. QString strText = parent->text(0);
    10.  
    11. CDADcb::CSignal* pSigs = items[0]->data( 0, Qt::UserRole ).value<CDADcb::CSignal*>();
    12. CDADcb::CMessage* pMess = parent->data( 0, Qt::UserRole ).value<CDADcb::CMessage*>();
    13.  
    14. if( pSigs ) // make sure we haven't selected message node
    15. {
    16. for( int nCount = 0; nCount < pMess->m_oSignals.count(); nCount++ )
    17. {
    18. CDADcb::CSignal* pSignal = new CDADcb::CSignal();
    19. pSignal->m_strId = pMess->m_strId;
    20. pSignal->m_strRawData = pMess->m_strRawData;
    21. CDADcb::ASignal* pSig = (CDADcb::ASignal*)pMess->m_oSignals.at(nCount);
    22. if( strcmp( pSigs->name, pSig->name ) == 0 )
    23. {
    24. strcpy_s( pSignal->name, pSig->name );
    25. pSignal->m_nCount = 0;
    26. strcpy_s( pSignal->unit, pSig->unit );
    27. strcpy_s( pSignal->canname, pMess->name );
    28. theApp->m_dcb.GetSignalList()->insert( 0, (QObject*)pSignal );
    29.  
    30. // we use this for fast look ups later in the model
    31. theApp->m_dcb.m_oSignalMap.insert( pMess->m_strId, (QObject*)pSignal );
    32. break;
    33. }
    34. }
    35. }
    36. }
    To copy to clipboard, switch view to plain text mode 

    The only problem I notice is that when I drag the item from the tree widget to the table view, the item is added before I've done a drop, but I guess this is because that is what I'm doing with the function above, if only I could do this on the models dropMimeData function...

    Regards,
    Steve

  8. #28
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: Drag and Drop, dropEvent not being called?

    The code is indeed strange. Could you show your dropMimeData() method of the model?

  9. #29
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag and Drop, dropEvent not being called?

    Hi,

    The code for my dropMimeData within my model doesn't actually do anything, this is where I would like to do the before mentioned function so the item is added on a drop event, but I have no way of getting the message/signal object from within that function, or is there away???

    Thanks,
    Steve

  10. #30
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: Drag and Drop, dropEvent not being called?

    As I mentioned before, it'd be best if Signal was serializable through QDataStream. Then you could just transfer it through the existing capabilities of QMimeData. Alternatively you can do this:
    Qt Code:
    1. class SignalMimeData : public QMimeData {
    2. //...
    3. setPointer(Signal *s){ ... }
    4. signal() const { ... }
    5. };
    To copy to clipboard, switch view to plain text mode 
    And in dropMimeData cast to SignalMimeData and retrieve the pointer.

  11. #31
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag and Drop, dropEvent not being called?

    Thanks for that,

    So, within my tree widget mimeData, I can just get CSignal and just set the pointer to it in the derived QMimeData class? Would mimeData return SignalMimeData, I'm just confused as to how I set this up?

    Kind regards,
    Steve

  12. #32
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: Drag and Drop, dropEvent not being called?

    You can, but I wouldn't do that unless you're sure the pointer will be valid after some time (after the model gets updated a few times).

    Yes, mimeData should return the subclass instance then.

  13. #33
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag and Drop, dropEvent not being called?

    Hi,

    I guess I also will have to return my derived mimeData from the mimeData functions?

    For some reason, my dropMimeData doesn't get called now?

    Regards,
    Steve

  14. #34
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: Drag and Drop, dropEvent not being called?

    Let's make it simple - everywhere where it says "QMimeData", use the subclass. Just don't change the function signatures, QMimeData has to remain there.

  15. #35
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag and Drop, dropEvent not being called?

    Ok, what I did was to change ALL the QMimeData within my functions to my own type and left the function signatures as QMimeData, still doesn't get called?

  16. #36
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: Drag and Drop, dropEvent not being called?

    Could you show me the whole model class?

  17. #37
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag and Drop, dropEvent not being called?

    Hi,

    I have now got this working thanks, it was the fact that I had overridden the retrieveData method of QMimeData in my subclassed version of this...I'm learning every day

    Many, many thanks for all your invaluable help, it is much appreciated.

    Regards,
    Steve
    Last edited by steg90; 22nd May 2007 at 07:22.

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.