Hello, I'm trying to detect a mouse dragging across the screen but despite subclassing QGraphicsScene and over-writing dragMoveEvent(), it doesn't seem like my mouse dragging is being detected (The debug string isn't being executed). I'm not looking for drag and drop functionality, I'm just trying to customize what happens in the scene when a mouse is dragged across. Basically, I need the whole scene to shift as the mouse is dragged such that it's like the mouse is dragging the scene around with all of its images, kind of like how adobe's pdf reader can drag around the document when the mouse is dragged. I was thinking about changing the image's positions in the scene and not the scene itself.

In implementing this, I was thinking about detecting the mouse drag event and then changing a global variable (Just for testing) that denotes the offset, and then this offset is communicated to all the Qpixmaps on the scene through the advance() function which sets the image's individual positions. The scene needs to be "scrollable" in such a way that the image needs to be freed when it goes off the screen too far, and the new images on the other side that are coming into the scene(or viewport or whatever the thing you see is called) are loaded and shown.

I know it'd be possible to implement scrollbars and do all that funky jazz to get easy scroll functionality but scroll-bars don't make sense for my application.



Qt Code:
  1. #include <QApplication>
  2. #include <QGraphicsEllipseItem>
  3. #include <QGraphicsScene>
  4. #include <QGraphicsView>
  5.  
  6. #include <qdatetime.h>
  7. #include <qmainwindow.h>
  8. #include <qstatusbar.h>
  9. #include <qmessagebox.h>
  10. #include <qmenubar.h>
  11. #include <qapplication.h>
  12. #include <qpainter.h>
  13. #include <qprinter.h>
  14. #include <qlabel.h>
  15. #include <qimage.h>
  16. #include <qpixmap.h>
  17. #include <QMouseEvent>
  18. #include <QStyleOptionGraphicsItem>
  19. #include <qdebug.h>
  20. #include <stdlib.h>
  21. #include <qtimer.h>
  22.  
  23. QString myImgName = "myimg.png";
  24. static QImage *myImg;
  25.  
  26.  
  27. int xOffset;
  28. int yOffset;
  29.  
  30.  
  31. static const int imageRTTI = 984376;
  32.  
  33. void debug(QString s)
  34. {
  35. qDebug(s.toAscii());
  36. }
  37.  
  38. class mapScene: public QGraphicsScene
  39. {
  40. public:
  41. mapScene();
  42.  
  43. QPoint prevMousePoint;
  44. QPoint currMousePoint;
  45. protected:
  46. void dragMoveEvent ( QGraphicsSceneDragDropEvent * event );
  47. };
  48.  
  49. mapScene::mapScene()
  50. {
  51. xOffset =0;
  52. yOffset =0;
  53. }
  54.  
  55.  
  56. void mapScene::dragMoveEvent( QGraphicsSceneDragDropEvent * event)
  57. {
  58. debug("I vus here!");
  59. xOffset = 1;
  60. yOffset = 1;
  61. }
  62.  
  63.  
  64.  
  65. class ImageItem: public QGraphicsRectItem
  66. {
  67. public:
  68. ImageItem( QImage img );
  69. int rtti () const { return imageRTTI; }
  70. void advance(int phase);
  71. protected:
  72. void paint( QPainter *, const QStyleOptionGraphicsItem *option, QWidget *widget );
  73. private:
  74. QImage image;
  75. QPixmap pixmap;
  76. int state;
  77. };
  78.  
  79.  
  80. void ImageItem::advance(int phase)
  81. {
  82. moveBy(xOffset,yOffset);
  83. }
  84.  
  85.  
  86. ImageItem::ImageItem( QImage img )
  87. : image(img)
  88. {
  89. state = 0;
  90. setRect(0, 0, image.width(), image.height());
  91. setFlag(ItemIsMovable);
  92. #if !defined(Q_WS_QWS)
  93. pixmap = pixmap.fromImage(image, Qt::OrderedAlphaDither);
  94. #endif
  95. }
  96.  
  97. void ImageItem::paint( QPainter *p, const QStyleOptionGraphicsItem *option, QWidget * )
  98. {
  99. // On Qt/Embedded, we can paint a QImage as fast as a QPixmap,
  100. // but on other platforms, we need to use a QPixmap.
  101. #if defined(Q_WS_QWS)
  102. p->drawImage( option->exposedRect, image, option->exposedRect, Qt::OrderedAlphaDither );
  103. #else
  104. p->drawPixmap( option->exposedRect, pixmap, option->exposedRect );
  105. #endif
  106. }
  107.  
  108. int main( int argc, char **argv )
  109. {
  110.  
  111. QApplication app(argc, argv);
  112.  
  113. mapScene scene;
  114. scene.setSceneRect( -100.0, -100.0, 200.0, 200.0 );
  115.  
  116. myImg = new QImage;
  117. myImg->load(myImgName);
  118.  
  119.  
  120. QAbstractGraphicsShapeItem* i = new ImageItem(*myImg);
  121. scene.addItem(i);
  122. i->setPos(-50,-50);
  123.  
  124. QTimer timer;
  125. QObject::connect(&timer, SIGNAL(timeout()), &scene, SLOT(advance()));
  126. timer.start(100);
  127.  
  128. QGraphicsView view( &scene );
  129. view.setRenderHints( QPainter::Antialiasing );
  130. view.show();
  131.  
  132. return app.exec();
  133. }
To copy to clipboard, switch view to plain text mode