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);
private:
// QMimeData* m_qMimeData;
// QDrag * m_qDrag;
int m_iType;
};
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;
};
To copy to clipboard, switch view to plain text mode
and mousePressEvent() function is defined as:
void CQtShapesLabel
::mousePressEvent(QMouseEvent *event
) {
dataStream << pixmap <<
QPoint(event
->pos
());
m_qMimeData->setData("application/x-dnditemdata", itemData);
m_qDrag->setMimeData(m_qMimeData);
m_qDrag->setPixmap(pixmap);
m_qDrag->setHotSpot(event->pos());
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);
}
}
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);
}
}
To copy to clipboard, switch view to plain text mode
My inherited QGraphicsView class looks like:
{
Q_OBJECT
protected:
//void paintEvent(QPaintEvent *event);
// void mousePressEvent(QMouseEvent *event);
public:
CImageView
( QWidget *parent
= 0,
unsigned int idx
= 0 );
virtual ~CImageView( );
};
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( );
};
To copy to clipboard, switch view to plain text mode
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
Bookmarks