PDA

View Full Version : QT Drag and Drop a QLabel into a QGraphicsView, how to?



jiapei100
5th March 2013, 10:18
Hi, all:

Sorry to bug you once again.

I'm trying to drag and drop a QLabel into a QGraphicsView.

My inherited label class looks like:



class CQtShapesLabel : public QLabel
{
Q_OBJECT


public:


explicit CQtShapesLabel(QWidget *parent = 0, int type = 0) : QLabel(parent), m_iType(type)
{
// this->m_qMimeData = NULL;
// this->m_qDrag = NULL;
}


virtual ~CQtShapesLabel() {}


protected:
// void dragEnterEvent(QDragEnterEvent *event);
// void dragMoveEvent(QDragMoveEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);


private:
// QMimeData* m_qMimeData;
// QDrag * m_qDrag;
int m_iType;
};




and mousePressEvent() function is defined as:

void CQtShapesLabel::mousePressEvent(QMouseEvent *event)
{
QPixmap pixmap = *this->pixmap();

QByteArray itemData;
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
dataStream << pixmap << QPoint(event->pos());

QMimeData* m_qMimeData = new QMimeData;
m_qMimeData->setData("application/x-dnditemdata", itemData);

QDrag* m_qDrag = new QDrag(this);
m_qDrag->setMimeData(m_qMimeData);
m_qDrag->setPixmap(pixmap);
m_qDrag->setHotSpot(event->pos());

QPixmap tempPixmap = pixmap;
QPainter painter;
painter.begin(&tempPixmap);
painter.fillRect(pixmap.rect(), QColor(127, 127, 127, 127));
painter.end();

this->setPixmap(tempPixmap);

if (m_qDrag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction)
this->close();
else
{
this->show();
this->setPixmap(pixmap);
}
}


My inherited QGraphicsView class looks like:


class QTVIEWS_EXPORT CImageView : public QGraphicsView
{
Q_OBJECT




protected:
//void paintEvent(QPaintEvent *event);
void dragEnterEvent(QDragEnterEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void dragLeaveEvent(QDragLeaveEvent *event);
void dropEvent(QDropEvent *event);
// void mousePressEvent(QMouseEvent *event);




public:


CImageView( QWidget *parent = 0, unsigned int idx = 0 );
virtual ~CImageView( );


};



When testing, I tried to print out something in all functions, but no matter how I tried, I found

CQtShapesLabel::mouseReleaseEvent(QMouseEvent *event); // understandable
CImageView::dragLeaveEvent(QDragLeaveEvent *event); // should be able to response when the drag leaves this view
CImageView::dropEvent(QDropEvent *event); // should be able to response when the drag tries to drop something
CImageView::dragMoveEvent(QDragMoveEvent *event) // should be able to response when the drag keeps moving inside the view


never responses. Why is it so? Can anybody help to explain please....


Best Regards
Pei

wysota
5th March 2013, 10:51
What about dragEnterEvent?

thomas@itest
5th March 2013, 10:52
from the doc : if(QGraphicsView::dragMode() == QGraphicsView::NoDrag) Nothing happens; the mouse event is ignored.

jiapei100
6th March 2013, 01:40
dragEnterEvent works perfectly here.



What about dragEnterEvent?

wysota
6th March 2013, 09:22
dragEnterEvent works perfectly here.

Do you accept the event in dragEnterEvent? If the event is accepted, does dragMoveEvent work? For other drag events to work, you need to accept a dragEnterEvent.

jiapei100
6th March 2013, 10:16
Wow... Thank you so much wysota.
You are always able to answer my questions !! After accept the dragEnterEvent, everything is working fine now !!!


Thank you so much !!


Best Regards
Pei


Do you accept the event in dragEnterEvent? If the event is accepted, does dragMoveEvent work? For other drag events to work, you need to accept a dragEnterEvent.