Results 1 to 6 of 6

Thread: QT Drag and Drop a QLabel into a QGraphicsView, how to?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2009
    Location
    Surrey, BC, Canada
    Posts
    110
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QT Drag and Drop a QLabel into a QGraphicsView, how to?

    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:
    Qt Code:
    1. class CQtShapesLabel : public QLabel
    2. {
    3. Q_OBJECT
    4.  
    5.  
    6. public:
    7.  
    8.  
    9. explicit CQtShapesLabel(QWidget *parent = 0, int type = 0) : QLabel(parent), m_iType(type)
    10. {
    11. // this->m_qMimeData = NULL;
    12. // this->m_qDrag = NULL;
    13. }
    14.  
    15.  
    16. virtual ~CQtShapesLabel() {}
    17.  
    18.  
    19. protected:
    20. // void dragEnterEvent(QDragEnterEvent *event);
    21. // void dragMoveEvent(QDragMoveEvent *event);
    22. void mousePressEvent(QMouseEvent *event);
    23. void mouseReleaseEvent(QMouseEvent *event);
    24.  
    25.  
    26. private:
    27. // QMimeData* m_qMimeData;
    28. // QDrag * m_qDrag;
    29. int m_iType;
    30. };
    To copy to clipboard, switch view to plain text mode 


    and mousePressEvent() function is defined as:
    Qt Code:
    1. void CQtShapesLabel::mousePressEvent(QMouseEvent *event)
    2. {
    3. QPixmap pixmap = *this->pixmap();
    4.  
    5. QByteArray itemData;
    6. QDataStream dataStream(&itemData, QIODevice::WriteOnly);
    7. dataStream << pixmap << QPoint(event->pos());
    8.  
    9. QMimeData* m_qMimeData = new QMimeData;
    10. m_qMimeData->setData("application/x-dnditemdata", itemData);
    11.  
    12. QDrag* m_qDrag = new QDrag(this);
    13. m_qDrag->setMimeData(m_qMimeData);
    14. m_qDrag->setPixmap(pixmap);
    15. m_qDrag->setHotSpot(event->pos());
    16.  
    17. QPixmap tempPixmap = pixmap;
    18. QPainter painter;
    19. painter.begin(&tempPixmap);
    20. painter.fillRect(pixmap.rect(), QColor(127, 127, 127, 127));
    21. painter.end();
    22.  
    23. this->setPixmap(tempPixmap);
    24.  
    25. if (m_qDrag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction)
    26. this->close();
    27. else
    28. {
    29. this->show();
    30. this->setPixmap(pixmap);
    31. }
    32. }
    To copy to clipboard, switch view to plain text mode 


    My inherited QGraphicsView class looks like:
    Qt Code:
    1. class QTVIEWS_EXPORT CImageView : public QGraphicsView
    2. {
    3. Q_OBJECT
    4.  
    5.  
    6.  
    7.  
    8. protected:
    9. //void paintEvent(QPaintEvent *event);
    10. void dragEnterEvent(QDragEnterEvent *event);
    11. void dragMoveEvent(QDragMoveEvent *event);
    12. void dragLeaveEvent(QDragLeaveEvent *event);
    13. void dropEvent(QDropEvent *event);
    14. // void mousePressEvent(QMouseEvent *event);
    15.  
    16.  
    17.  
    18.  
    19. public:
    20.  
    21.  
    22. CImageView( QWidget *parent = 0, unsigned int idx = 0 );
    23. virtual ~CImageView( );
    24.  
    25.  
    26. };
    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
    Last edited by jiapei100; 5th March 2013 at 10:22.
    Welcome to Vision Open
    http://www.visionopen.com

Similar Threads

  1. Replies: 2
    Last Post: 10th October 2012, 11:33
  2. Drag from QtreeWidget and drop to QGraphicsView
    By syjgin in forum Qt Programming
    Replies: 0
    Last Post: 3rd July 2010, 14:17
  3. How to drag and drop on QGraphicsView frame
    By wudelei in forum Qt Programming
    Replies: 1
    Last Post: 16th April 2010, 08:04
  4. Drag and Drop from QListView to QGraphicsView
    By NoRulez in forum Qt Programming
    Replies: 0
    Last Post: 20th August 2009, 14:26
  5. Drag and drop from QTreeWidget to QGraphicsView
    By igu@na in forum Qt Programming
    Replies: 2
    Last Post: 27th August 2008, 08:24

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
  •  
Qt is a trademark of The Qt Company.