Results 1 to 10 of 10

Thread: Detecting and reacting to mouse dragging in QGraphicsView?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2009
    Posts
    23
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanked 1 Time in 1 Post

    Default

    Ok, so I'm trying to do things with the MousePress, MouseRelease and MouseMove events and they all seem to work as far as they seem like they're literally supposed to work, but I use a custom boolean "mouseDown" to detect if the mouse is down and realized I need to process code whenever the mouse is down, whether moving or not. MousePress only triggers on a mouse press, MouseRelease only triggers when it's released, and MouseMove only triggers when the mouse is moved. How can I implement something that's triggered whenever the custom boolean "mouseDown" is true (or any condition is true for that matter)?

    Ok, I never did implement the "execute whenever mousedown" is true, but I found another way around the problem I was having. When I move the mouse with the button down and then stopped, the "change x" variable of the image didn't go to zero (i.e. the image kept moving), so I needed to find a way to detect when the mouse stopped moving and that's what the mouseDown boolean was for.

    However, I found that just by resetting the "change x" variable of the image to zero after advancing the image with "advance()" did what I wanted to. So no problems there.

    Now, I need to make it "unchoppy" and the image seems to "slip" in regard to the mouse moving when I move "fast". I think I need advance() to trigger whenever the mouse is moved instead of on a fixed time interval... how would I do that? I'm thinking it has something to do with slots.

    wait, would it be as simple as calling advance() inside the mouseMove event function? Can I even call advance() inside the QGraphicsScene class? *investigates*

    Woah! I just found that I could call advance() directly inside the QGraphicsScene subclass and now it's truly dragging! Here's my code just in case some other n00b might benefit from it.

    Qt Code:
    1. //#include <Q4MemArray>
    2. //#include "canvas.h"
    3. //#include <q4progressdialog.h>
    4. //#include <Q4PointArray>
    5. //#include <Q4PtrList>
    6. //#include <QPixmap>
    7. //#include "mapscene.h"
    8. //#include <Q4PopupMenu>
    9.  
    10. #include <QApplication>
    11. #include <QGraphicsEllipseItem>
    12. #include <QGraphicsScene>
    13. #include <QGraphicsView>
    14. #include <QtGui>
    15.  
    16. #include <qdatetime.h>
    17. #include <qmainwindow.h>
    18. #include <qstatusbar.h>
    19. #include <qmessagebox.h>
    20. #include <qmenubar.h>
    21. #include <qapplication.h>
    22. #include <qpainter.h>
    23. #include <qprinter.h>
    24. #include <qlabel.h>
    25. #include <qimage.h>
    26. #include <qpixmap.h>
    27. #include <QMouseEvent>
    28. #include <QStyleOptionGraphicsItem>
    29. #include <qdebug.h>
    30. #include <stdlib.h>
    31. #include <qtimer.h>
    32.  
    33.  
    34. QString myImgName = "myimg.png";
    35. static QImage *myImg;
    36.  
    37. int xOffset;
    38. int yOffset;
    39.  
    40. static const int imageRTTI = 984376;
    41.  
    42. void debug(QString s)
    43. {
    44. qDebug(s.toAscii());
    45. }
    46.  
    47. class mapScene: public QGraphicsScene
    48. {
    49. public:
    50. mapScene();
    51.  
    52. int mouseMoveX;
    53. int mouseMoveY;
    54. QPointF prevMousePoint;
    55. QPointF currMousePoint;
    56.  
    57. bool mouseDown;
    58. protected:
    59. void mouseMoveEvent ( QGraphicsSceneMouseEvent * mouseEvent );
    60. void mousePressEvent ( QGraphicsSceneMouseEvent * mouseEvent );
    61. void mouseReleaseEvent ( QGraphicsSceneMouseEvent * mouseEvent );
    62.  
    63. };
    64.  
    65. mapScene::mapScene()
    66. {
    67. mouseDown = false;
    68. mouseMoveX = 0;
    69. mouseMoveY = 0;
    70. xOffset =0;
    71. yOffset =0;
    72.  
    73. }
    74.  
    75. void mapScene::mouseMoveEvent( QGraphicsSceneMouseEvent * event)
    76. {
    77. if(mouseDown)
    78. {
    79. QString dummy;
    80. debug("mommy");
    81. /*
    82.   debug(dummy.setNum(event->pos().x()));
    83.   debug(dummy.setNum(event->lastPos().x()));
    84.   debug(dummy.setNum(event->lastScenePos().x()));
    85.   debug(dummy.setNum(event->scenePos().x()));
    86.   */
    87. xOffset = (int)(event->scenePos().x() - event->lastScenePos().x());
    88. yOffset = (int)(event->scenePos().y() - event->lastScenePos().y());
    89. advance();
    90. }
    91. else
    92. {
    93. xOffset = 0;
    94. yOffset = 0;
    95. }
    96. }
    97.  
    98. void mapScene::mousePressEvent( QGraphicsSceneMouseEvent * event)
    99. {
    100. mouseDown = true;
    101. }
    102.  
    103. void mapScene::mouseReleaseEvent( QGraphicsSceneMouseEvent * event)
    104. {
    105. mouseDown = false;
    106. xOffset = 0;
    107. yOffset = 0;
    108. }
    109.  
    110.  
    111.  
    112. class ImageItem: public QGraphicsRectItem
    113. {
    114. public:
    115. ImageItem( QImage img );
    116. int rtti () const { return imageRTTI; }
    117. void advance(int phase);
    118. protected:
    119. void paint( QPainter *, const QStyleOptionGraphicsItem *option, QWidget *widget );
    120. private:
    121. QImage image;
    122. QPixmap pixmap;
    123. int state;
    124. };
    125.  
    126.  
    127. void ImageItem::advance(int phase)
    128. {
    129.  
    130. moveBy(xOffset,yOffset);
    131. xOffset = 0; yOffset = 0;
    132. }
    133.  
    134.  
    135. ImageItem::ImageItem( QImage img )
    136. : image(img)
    137. {
    138. state = 0;
    139. setRect(0, 0, image.width(), image.height());
    140. setFlag(ItemIsMovable);
    141. #if !defined(Q_WS_QWS)
    142. pixmap = pixmap.fromImage(image, Qt::OrderedAlphaDither);
    143. #endif
    144. }
    145.  
    146. void ImageItem::paint( QPainter *p, const QStyleOptionGraphicsItem *option, QWidget * )
    147. {
    148. // On Qt/Embedded, we can paint a QImage as fast as a QPixmap,
    149. // but on other platforms, we need to use a QPixmap.
    150. #if defined(Q_WS_QWS)
    151. p->drawImage( option->exposedRect, image, option->exposedRect, Qt::OrderedAlphaDither );
    152. #else
    153. p->drawPixmap( option->exposedRect, pixmap, option->exposedRect );
    154. #endif
    155. }
    156.  
    157. int main( int argc, char **argv )
    158. {
    159.  
    160. QApplication app(argc, argv);
    161.  
    162. mapScene scene;//mapScene scene;
    163. scene.setSceneRect( -500.0, -500.0, 1000.0, 1000.0 );//( -100.0, -100.0, 200.0, 200.0 );
    164.  
    165. myImg = new QImage;
    166. myImg->load(myImgName);
    167.  
    168.  
    169. QAbstractGraphicsShapeItem* i = new ImageItem(*myImg);
    170. scene.addItem(i);
    171. i->setPos(-50,-50);
    172.  
    173.  
    174.  
    175. QGraphicsView view( &scene );
    176. // view.setDragMode(QGraphicsView::ScrollHandDrag);
    177. view.setRenderHints( QPainter::Antialiasing );
    178. view.show();
    179.  
    180. return app.exec();
    181. }
    To copy to clipboard, switch view to plain text mode 

    I'm now trying to get this dragging behavior to work for multiple images all at the same time but only one image (coindentally, the first one that I added to the scene) moves, but if the first one goes off the screen, then the second one moves, albeit at a slower rate than the mouse moving meaning it's only getting triggered to move a part of the time the mouse is moved. It seems this is sufficiently different to deserve its own thread, so I'm posting that issue at http://www.qtcentre.org/forum/f-qt-p...ene-25083.html .
    Last edited by wysota; 26th October 2009 at 08:24. Reason: If you are replying to your own posts, please edit the original post instead

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.