Here is a working example.
Here is a working example.
Many thanks, that is very kind of you.
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
I think we told you a few times to remove the mouse event handlers
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.Is it possible to pass over the object associated with the item being dragged from the tree widget?
Hi again,
This is my mimeData function within the treewidget :
Qt Code:
{ if(items.isEmpty()) return 0; CDADcb::CSignal* pSignal = items[0]->data( 0, Qt::UserRole ).value<CDADcb::CSignal*>(); CDADcb::CMessage* pMess = parent->data( 0, Qt::UserRole ).value<CDADcb::CMessage*>(); md->setData("application/x-example", items[0]->data(0, Qt::DisplayRole).toByteArray()); return md; }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
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?
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:
{ if(items.isEmpty()) return 0; if( parent ) { CDADcb::CSignal* pSigs = items[0]->data( 0, Qt::UserRole ).value<CDADcb::CSignal*>(); CDADcb::CMessage* pMess = parent->data( 0, Qt::UserRole ).value<CDADcb::CMessage*>(); if( pSigs ) // make sure we haven't selected message node { for( int nCount = 0; nCount < pMess->m_oSignals.count(); nCount++ ) { CDADcb::CSignal* pSignal = new CDADcb::CSignal(); pSignal->m_strId = pMess->m_strId; pSignal->m_strRawData = pMess->m_strRawData; CDADcb::ASignal* pSig = (CDADcb::ASignal*)pMess->m_oSignals.at(nCount); if( strcmp( pSigs->name, pSig->name ) == 0 ) { strcpy_s( pSignal->name, pSig->name ); pSignal->m_nCount = 0; strcpy_s( pSignal->unit, pSig->unit ); strcpy_s( pSignal->canname, pMess->name ); // we use this for fast look ups later in the model break; } } } }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
The code is indeed strange. Could you show your dropMimeData() method of the model?
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
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:
And in dropMimeData cast to SignalMimeData and retrieve the pointer.Qt Code:
//... setPointer(Signal *s){ ... } signal() const { ... } };To copy to clipboard, switch view to plain text mode
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
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.
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
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.
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?
Could you show me the whole model class?
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