Results 1 to 3 of 3

Thread: Dragging a rectangle across a QGraphicsView

  1. #1
    Join Date
    Nov 2010
    Posts
    77
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Dragging a rectangle across a QGraphicsView

    I have a rectangle that I want to drag across a graphics view.

    Qt Code:
    1. #ifndef BLOCK_H
    2. #define BLOCK_H
    3.  
    4. #include <QGraphicsRectItem>
    5.  
    6. class Block : public QGraphicsRectItem
    7. {
    8. public:
    9. Block(int x, int y);
    10.  
    11. // GUI events
    12. void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    13. };
    14.  
    15. #endif // BLOCK_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QPen>
    2. #include <QLineF>
    3. #include <QBrush>
    4. #include <QApplication>
    5. #include <QGraphicsRectItem>
    6. #include <QGraphicsSceneEvent>
    7. #include <iostream>
    8. #include "block.h"
    9.  
    10. Block::Block(int x, int y) {
    11. this->setRect(x, y, 10, 10);
    12. this->setBrush(QBrush(QColor(0, 0, 0)));
    13. this->setPen(QPen(QColor(0, 0, 0)));
    14. }
    15.  
    16. void Block::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
    17. // Is move above threshold?
    18. if (QLineF(event->screenPos(), event->buttonDownScreenPos(Qt::LeftButton)).length() < QApplication::startDragDistance())
    19. return;
    20.  
    21. // Update x and y
    22. this->setPos(event->scenePos().x(), event->scenePos().y());
    23. }
    To copy to clipboard, switch view to plain text mode 

    Edit: I already figured out that I should be using scene coordinates, but when I initiate a drag the selected element doesn't stick to the top left of my mouse pointer as shown here:
    http://www.screentoaster.com/watch/s...X1BR/dragndrop

    How can I fix this?
    Last edited by blooglet; 14th November 2010 at 14:36.

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Dragging a rectangle across a QGraphicsView

    Make it more simple, check enum QGraphicsItem::ItemIsMovable.

  3. The following user says thank you to MarekR22 for this useful post:

    blooglet (16th November 2010)

  4. #3
    Join Date
    Nov 2010
    Posts
    77
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dragging a rectangle across a QGraphicsView

    D'oh... of course. Thanks!

Similar Threads

  1. Replies: 1
    Last Post: 9th December 2009, 08:44
  2. Replies: 9
    Last Post: 26th October 2009, 00:13
  3. QGraphicsView : change selected rectangle style
    By kghose in forum Qt Programming
    Replies: 2
    Last Post: 28th July 2008, 18:12
  4. dragging
    By mickey in forum Newbie
    Replies: 1
    Last Post: 25th July 2006, 14:05
  5. dragging
    By mickey in forum Newbie
    Replies: 5
    Last Post: 11th March 2006, 00:26

Tags for this Thread

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.