Results 1 to 3 of 3

Thread: real world coordinates in graphics view / scene

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2007
    Posts
    26
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default real world coordinates in graphics view / scene

    Hello,

    I'm trying to figure out the best way to retain the real world coordinates of items in a graphics scene. i.e so that items can be added at any stage as required in there correct world coordinates (like in a cad program).

    I have a subclassed QGraphics view with some mouse events for panning / zooming round and display the cursor position (in scene coordinates) in a QLabel, but am struggling to get my head around the scene coordinates which are displayed when I am simply scrolling (panning) around the view.

    They Y axis scene coordinates behave as expected but I cant figure out whats going on with the X axis coordinates (why do they change when I am just scrolling around? (drag with middle mouse button).

    For example:

    Qt Code:
    1. #include <QApplication>
    2. #include <QMainWindow>
    3. #include <QStatusBar>
    4. #include <QGraphicsView>
    5. #include <QGraphicsScene>
    6. #include <QGraphicsTextItem>
    7. #include <QMouseEvent>
    8. #include <QScrollBar>
    9. #include <QLabel>
    10. #include <math.h>
    11.  
    12. class MapView : public QGraphicsView
    13. {
    14.  
    15. public:
    16. MapView(QWidget *parent = 0);
    17. QPoint sceneCoordinates;
    18. QLabel *coordinateDisplayLabel;
    19.  
    20. protected:
    21. void scaleView(qreal scaleFactor);
    22. void wheelEvent(QWheelEvent *event);
    23. void mousePressEvent(QMouseEvent *event);
    24. void mouseMoveEvent(QMouseEvent *event);
    25. void mouseReleaseEvent(QMouseEvent *event);
    26.  
    27. private:
    28. QString cursorXString;
    29. QString cursorYString;
    30. void storeMouseEvent(QMouseEvent *event);
    31. bool scrolling;
    32. QMouseEvent lastMouseEvent;
    33.  
    34. };
    35.  
    36. MapView::MapView(QWidget *parent)
    37. : QGraphicsView(parent), lastMouseEvent(QEvent::None,QPoint(),
    38. Qt::NoButton,Qt::NoButton,Qt::NoModifier)
    39.  
    40. {
    41. setAttribute (Qt::WA_SetCursor);
    42. setCursor(Qt::CrossCursor);
    43. scale(1,-1);
    44. coordinateDisplayLabel = new QLabel(0);
    45. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    46. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    47. setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
    48. scrolling = false;
    49. }
    50.  
    51. void MapView::wheelEvent(QWheelEvent *event)
    52. {
    53. scaleView(pow((double)2, event->delta() / 240.0));
    54. }
    55.  
    56. void MapView::scaleView(qreal scaleFactor)
    57. {
    58. qreal factor = matrix().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
    59. if (factor < 0.07 || factor > 100)
    60. return;
    61. scale(scaleFactor, scaleFactor);
    62. }
    63.  
    64. void MapView::storeMouseEvent(QMouseEvent *event)
    65. {
    66. lastMouseEvent = QMouseEvent(event->type(), event->pos(), event->globalPos(),
    67. event->button(),event->buttons(),event->modifiers());
    68. }
    69.  
    70. void MapView::mousePressEvent(QMouseEvent *event)
    71. {
    72. if(event->button() == Qt::MidButton)
    73. {
    74. setCursor(Qt::ClosedHandCursor);
    75. scrolling = true;
    76. storeMouseEvent(event);
    77. event->accept();
    78. return;
    79. }
    80. }
    81.  
    82. void MapView::mouseMoveEvent(QMouseEvent *event)
    83. {
    84. if(scrolling)
    85. {
    86. QPoint delta = event->globalPos() - lastMouseEvent.globalPos();
    87. QScrollBar *hBar = horizontalScrollBar();
    88. QScrollBar *vBar = verticalScrollBar();
    89. hBar->setValue(hBar->value() - delta.x());
    90. vBar->setValue(vBar->value() - delta.y());
    91. storeMouseEvent(event);
    92. event->accept();
    93. return;
    94. }
    95.  
    96. sceneCoordinates = mapFromScene(event->pos());
    97. cursorXString.setNum(sceneCoordinates.x());
    98. cursorYString.setNum(sceneCoordinates.y());
    99. coordinateDisplayLabel->setText("x = " + cursorXString + " " + "y = " + cursorYString);
    100. }
    101.  
    102. void MapView::mouseReleaseEvent(QMouseEvent *event)
    103. {
    104. if((event->button() == Qt::MidButton) && (scrolling == true))
    105. {
    106. setCursor(Qt::CrossCursor);
    107. scrolling = false;
    108. storeMouseEvent(event);
    109. event->accept();
    110. return;
    111. }
    112. event->ignore();
    113. }
    114.  
    115.  
    116. int main( int argc, char **argv )
    117. {
    118. QApplication a( argc, argv );
    119. QMainWindow *mw = new QMainWindow();
    120. MapView *view = new MapView;
    121. QStatusBar *statusbar = new QStatusBar;
    122.  
    123. scene->setSceneRect(-30000, -30000, 60000, 60000);
    124. scene->setBackgroundBrush(Qt::black);
    125.  
    126. scene->addLine ( -50, 0, 50, 0, (QPen(Qt::white, 0)));
    127. scene->addLine ( 0, -50, 0, 50, (QPen(Qt::white, 0)));
    128. QGraphicsTextItem item1("0,0");
    129. item1.setDefaultTextColor(Qt::white);
    130. item1.setPos(0,0);
    131. item1.scale(1,-1);
    132. scene->addItem(&item1);
    133.  
    134. scene->addLine ( 200, 250, 300, 250, (QPen(Qt::white, 0)));
    135. scene->addLine ( 250, 300, 250, 200, (QPen(Qt::white, 0)));
    136. QGraphicsTextItem item2("250,250");
    137. item2.setDefaultTextColor(Qt::white);
    138. item2.setPos(250,250);
    139. item2.scale(1,-1);
    140. scene->addItem(&item2);
    141.  
    142. view->setScene(scene);
    143. mw->setCentralWidget(view);
    144. statusbar->addPermanentWidget(view->coordinateDisplayLabel, 0);
    145. mw->setStatusBar(statusbar);
    146.  
    147. mw->show();
    148.  
    149. return a.exec();
    150. }
    To copy to clipboard, switch view to plain text mode 

    Ultimately I'm trying to get the coordinate display to always match the original scene coordinates.


    Hope someone can enlighten me a bit.


    Thanks
    Last edited by robertson1; 6th December 2007 at 23:11.

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.