Results 1 to 6 of 6

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

  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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

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

    What about dragEnterEvent?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2011
    Posts
    27
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    from the doc : if(QGraphicsView::dragMode() == QGraphicsView::NoDrag) Nothing happens; the mouse event is ignored.

  4. #4
    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 Re: QT Drag and Drop a QLabel into a QGraphicsView, how to?

    dragEnterEvent works perfectly here.


    Quote Originally Posted by wysota View Post
    What about dragEnterEvent?
    Welcome to Vision Open
    http://www.visionopen.com

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

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

    Quote Originally Posted by jiapei100 View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    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 Re: QT Drag and Drop a QLabel into a QGraphicsView, how to?

    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

    Quote Originally Posted by wysota View Post
    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.
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.