-
Drag and Drop, dropEvent not being called?
Hi,
I have a tree widget which allows items to be dragged. I have a derived tableview which accepts drops, I have reimplemented the dragEnterEvent and dropEvent in here, the dragEnterEvent gets called but the dropEvent doesn't. At the moment I am just accepting any types of drops.
Code in the tableview :
Code:
{
QString strText
= e
->mimeData
()->text
();
e->acceptProposedAction();
}
{
e->accept();
}
Any idea what I'm missing?
Regards,
Steve
-
Re: Drag and Drop, dropEvent not being called?
You have to handle this in the model, with dropMimeData.
Regards
-
Re: Drag and Drop, dropEvent not being called?
Hi,
Got this working now, I needed to also reimplement the dragMoveEvent. Just now need to figure out how to put the dragged item into my table model...
Regards,
Steve
-
Re: Drag and Drop, dropEvent not being called?
Thanks,
My tree consists of objects of type CSignal ( I use set data on the items for this ). When you drag an item from the tree ( CSignal ) I want to get this data as type CSignal, how do I do this? Is this a custom mime type?
Regards,
Steve
-
Re: Drag and Drop, dropEvent not being called?
Might I ask why do you reimplement methods from the view to handle this?
-
Re: Drag and Drop, dropEvent not being called?
I have done this as I've been following an example which does this? Is there a better way?!
I'm now trying to subclass QMimeData in order to have my own custom data.
Regards,
Steve
-
Re: Drag and Drop, dropEvent not being called?
Which example does that? I'm pretty sure all drag and drop examples that concern views reimplement mimeTypes(), mimeData() and dropMimeData() from the model.
-
Re: Drag and Drop, dropEvent not being called?
Hi,
Well the example ( C++ GUI Programming with Qt4) doesn't use a model so I guess this is why it handles the events in the view/widget. I have just implemented the dropMimeData method in my model but it doesn't get called, I guess this is because I have the events implemented in my view?!
Kind regards,
Steve
-
Re: Drag and Drop, dropEvent not being called?
Quote:
Originally Posted by
steg90
Well the example ( C++ GUI Programming with Qt4) doesn't use a model so I guess this is why it handles the events in the view/widget.
Yes, it doesn't have the required infrastructure which is already present in views.
Quote:
I have just implemented the dropMimeData method in my model but it doesn't get called, I guess this is because I have the events implemented in my view?!
Correct. You only need to reimplement the three methods mentioned, even if you want to carry custom data.
-
Re: Drag and Drop, dropEvent not being called?
Thanks,
I have removed the drag/drop events from my view and implemented the three methods you mentioned into my model, but they don't get called? Do you have to set up the model to accept drops or something?
Regards,
Steve
-
Re: Drag and Drop, dropEvent not being called?
got it - setSupportedDragActions !
mimeTypes gets called but dropMimeData and mimeData are not...
-
Re: Drag and Drop, dropEvent not being called?
Do you return drag and drop flags in flags()?
-
Re: Drag and Drop, dropEvent not being called?
Hi JPN,
I have the following flags function in my model code :
Code:
Qt
::ItemFlags DACanTreeModel
::flags(const QModelIndex &index
) const{
if (index.isValid())
return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
else
return Qt::ItemIsDropEnabled | defaultFlags;
}
Still can't get the dropMimeData function to be called...
-
Re: Drag and Drop, dropEvent not being called?
Also, the flags for the item in the treewidget I'm dragging are set to ItemIsDragEnabled. Can't understand why dropMimeData isn't being called...
Regards,
Steve
-
Re: Drag and Drop, dropEvent not being called?
Presumably you are already following the instructions in Using Drag and Drop with Item Views, because your flags() looks so similar. But does your view still have overridden drag/dragmove/drop event handlers? If so, I'd suggest commenting them out. For example accepting the event in dropEvent() and then calling the base class implementation makes it never drop..
-
Re: Drag and Drop, dropEvent not being called?
Also if you drag between two widgets, make sure they understand each other's mime-types.
-
Re: Drag and Drop, dropEvent not being called?
Hi,
Yes, following that example, I had removed the events from my view. Still not being called, got to be something simple?!
This is the code I have in my treewidget ( items in here I want to be draggable ) :
Code:
void DASignalTree
::mousePressEvent ( QMouseEvent * event
) {
if( event->button() == Qt::LeftButton )
{
m_startPos = event->pos();
}
}
void DASignalTree
::mouseMoveEvent( QMouseEvent * event
) {
if( event->buttons() & Qt::LeftButton )
{
int distance = ( event->pos() - m_startPos).manhattanLength();
{
if ( item )
{
startDrag();
}
}
}
// QTreeWidget::mouseMoveEvent( event );
}
void DASignalTree::startDrag()
{
DAMimeData* pMimeData = new DAMimeData;
pMimeData->setText( dragItem->text( 0 ) );
pDrag->setMimeData( pMimeData );
pDrag
->setPixmap
( QPixmap("images/new.png") );
theApp->AddText( dragItem->text( 0 ) );
if( pDrag->start(Qt::MoveAction) == Qt::MoveAction)
delete dragItem;
}
and the code in my model :
Code:
bool DACanTreeModel
::dropMimeData ( const QMimeData * data, Qt
::DropAction action,
int row,
int column,
const QModelIndex & parent
) {
return true;
}
{
}
QMimeData * DACanTreeModel
::mimeData ( const QModelIndexList
& indexes
) const {
}
Qt::DropActions DACanTreeModel::supportedDropActions() const
{
return Qt::CopyAction | Qt::MoveAction;
}
Qt
::ItemFlags DACanTreeModel
::flags(const QModelIndex &index
) const{
if (index.isValid())
return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
else
return Qt::ItemIsDropEnabled | defaultFlags;
}
And finally the view constructor :
Code:
DATableView
::DATableView(QWidget *parent
){
setAcceptDrops(true);
setDropIndicatorShown(true);
}
I'm at a loss?! :confused:
Regards,
Steve
-
Re: Drag and Drop, dropEvent not being called?
The tree widget also has the before mentioned methods which you should reimplement.
-
Re: Drag and Drop, dropEvent not being called?
Thanks,
So I need to also implement dropMimeData, mimeData and mimeTypes in my tree widget? I guess I can ignore the dropMimeData as my tree won't allow any drop events?
Regards,
Steve
-
Re: Drag and Drop, dropEvent not being called?
Implemented the methods mimeData, dropmimeData and mimeTypes in the tree widget, but still no dropMimeData being called...
When I was doing the drag/drop in the view, it seemed a lot easier and seemed to work, just can't understand what is wrong.
Regards,
Steve