Results 1 to 16 of 16

Thread: QGraphicsView coordinate system

  1. #1
    Join Date
    Feb 2011
    Posts
    20
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QGraphicsView coordinate system

    I´ve been reading about QGraphicsView/Scene/Item all day and still I fail to understand how it works. I started with QT Creator and added a QGraphicsView to the mainWindow. Then I subclassed QGraphicsScene and QGraphicsItem(I probably need to subclass the QGraphicsView later on). I use shapelib to open a shape-file and If I´ve done it right it will be implemented as a QGraphicsItem. I add the item to the scene and the scene to the view. I can see the map(shapefile) but it´s centered and very small. So I thought let´s change the coordinatesystem of the scene... And this is where I got stuck.
    I know the boundaries of the GraphicsView and the Scene. I also know the boundaries of the shapefile. Why can´t I tell the scene to have the same boundaries as the shapefile but extend it to the size of the view!?
    Maybe it isn´t hard but obviously I don´t have the brains for it so any help at all will be very appreciated.

    Code for subclassed QGraphicsItem

    Qt Code:
    1. item::item(QGraphicsItem *parent)
    2. : QGraphicsItem(parent)
    3. {
    4. }
    5.  
    6. QRectF item::boundingRect() const
    7. {
    8. QRectF recWindow;
    9. recWindow.setBottomLeft(QPointF(sBoundingBox.minX,sBoundingBox.minY));
    10. recWindow.setBottomRight(QPointF(sBoundingBox.maxX, sBoundingBox.minY));
    11. recWindow.setTopLeft(QPointF(sBoundingBox.minX,sBoundingBox.maxY));
    12. recWindow.setTopRight(QPointF(sBoundingBox.maxX,sBoundingBox.maxY));
    13. return recWindow;
    14. }
    15.  
    16. void item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    17. {
    18. OpenShapeFile("./Maps/Norden_wgs84.shp");
    19. painter->setRenderHint(QPainter::Antialiasing);
    20. painter->setBrush(Qt::blue);
    21.  
    22. for(int i = 0;i< vMap.size(); i++)
    23. {
    24. painter->drawPolyline(vMap.at(i)->p.data() ,vMap.at(i)->p.size());
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

    I´m not really doing anything in the subclassed QGraphicsScene yet so I don´t think there is any reason to show the code.

    And here is the mainWindow
    Qt Code:
    1. IceChart3::IceChart3(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. ui.setupUi(this);
    5.  
    6. QRect sceneRect;
    7. sceneRect.setBottomLeft(QPoint(4,52));
    8. sceneRect.setBottomRight(QPoint(42, 52));
    9. sceneRect.setTopLeft(QPoint(4,72));
    10. sceneRect.setTopRight(QPoint(42,72));
    11.  
    12. scene = new MyScene(this);
    13. scene->setSceneRect(sceneRect);
    14.  
    15. itemMap = new item();
    16.  
    17. scene->addItem(itemMap);
    18.  
    19. //Flip upside down.
    20. ui.graphicsView->scale(1,-1);
    21.  
    22. //Set scene to view
    23. ui.graphicsView->setScene(scene);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView coordinate system

    To zoom on a rect I use QGraphicsView::fitInView()

  3. #3
    Join Date
    Feb 2011
    Posts
    20
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsView coordinate system

    Hmm I tried it like this and it does nothing I'm afraid. Probably I'm not doing it right?

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. QRectF recWindow;
    8. recWindow.setBottomLeft(QPointF(4.,52.));
    9. recWindow.setBottomRight(QPointF(42., 52.));
    10. recWindow.setTopLeft(QPointF(4.,72.));
    11. recWindow.setTopRight(QPointF(42.,72.));
    12.  
    13. scene = new MyScene(this);
    14. test = new item();
    15.  
    16. scene->setSceneRect(recWindow);
    17. ui->graphicsView->fitInView(recWindow,Qt::IgnoreAspectRatio);
    18.  
    19. scene->addItem(test);
    20. ui->graphicsView->setScene(scene);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView coordinate system

    I did not try really but this should do the trick.
    by the way the rect you define does not seem valid, unless you invert top and bottom (like you scaled view's Y in first post)

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. scene = new MyScene(this);
    8. ui->graphicsView->setScene(scene);
    9.  
    10. test = new item();
    11. scene->addItem(test);
    12.  
    13. ui->graphicsView->fitInView(scene->itemsBoundingRect());
    14. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Feb 2011
    Posts
    20
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsView coordinate system

    That didn´t work either I´m afraid. I´ve managed to implement a zoom-function. And for a moment I saw a light in the tunnel actually...But this function only works halfway so to speak. After just a few rolls with the scrollwheel on the mouse everything turns white.

    Qt Code:
    1. void IceChart3::wheelEvent(QWheelEvent *event)
    2. {
    3. //Get the position of the mouse before scaling, in scene coords
    4. QPointF pointBeforeScale(ui.graphicsView->mapToScene(event->pos()));
    5.  
    6. //Get the original screen centerpoint
    7. QPointF screenCenter = GetCenter(); //CurrentCenterPoint; //(visRect.center());
    8.  
    9. double scaleFactor = 2.15; //How fast we zoom
    10. if(event->delta() > 0)
    11. {
    12. //Zoom in
    13. ui.graphicsView->scale(scaleFactor, scaleFactor);
    14. }
    15. else
    16. {
    17. //Zooming out
    18. ui.graphicsView->scale(1.0 / scaleFactor, 1.0 / scaleFactor);
    19. }
    20.  
    21. //Get the position after scaling, in scene coords
    22. QPointF pointAfterScale(ui.graphicsView->mapToScene(event->pos()));
    23.  
    24. //Get the offset of how the screen moved
    25. QPointF offset = pointBeforeScale - pointAfterScale;
    26.  
    27. //Adjust to the new center for correct zooming
    28. QPointF newCenter = screenCenter + offset;
    29. SetCenter(newCenter);
    30. }
    To copy to clipboard, switch view to plain text mode 

    Thank you totem by the way for taking the time to help me. If I could I´d by you a beer

  6. #6
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView coordinate system

    Quote Originally Posted by roband915 View Post
    That didn´t work either I´m afraid
    What happened then ?

    Speaking of zoom method, here is how I usually do :

    Qt Code:
    1. void MyView::wheelEvent(QWheelEvent *e)
    2. {
    3. const qreal zoomStep = 0.25 ;
    4. double zoomFactor = pow( 2.0, e->delta() / 8.0 / 15.0 * zoomStep ) ;
    5. scale(zoomFactor, zoomFactor);
    6.  
    7. e->setModifiers(Qt::NoModifier) ; // to avoid [CTRL+wheel] behave like QAbstractScrollArea, maybe not useful for everyone
    8. QGraphicsView::wheelEvent(e) ;
    9. }
    To copy to clipboard, switch view to plain text mode 

    Plus, you can choose to zoom on view's center or under cursor using QGraphiscView::transformationAnchor

  7. #7
    Join Date
    Feb 2011
    Posts
    20
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsView coordinate system

    Nothing really. The map(QGraphicsItem) is still centered and small.

    I tried your zoom method and I still get the same result. After just a few movements with the wheel then the map disappears.

  8. #8
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView coordinate system

    After adding an item to the scene, the following code should have worked

    Qt Code:
    1. ui->graphicsView->fitInView(scene->itemsBoundingRect());
    To copy to clipboard, switch view to plain text mode 

    Can you post your graphicsitem's code ? because I don't see were the problem comes from. Something must be wrong, like item's bounding rect or something

  9. #9
    Join Date
    Feb 2011
    Posts
    20
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsView coordinate system

    Hmm ok maybe like you said I´m doing something wrong in with the QGraphicsItem.
    Here is the .h file
    Qt Code:
    1. #ifndef ITEM_H
    2. #define ITEM_H
    3.  
    4. #include <QGraphicsItem>
    5. #include <QPointF>
    6. #include <QRectF>
    7. #include <QPolygonF>
    8. #include <QPainter>
    9. #include <QPainterPath>
    10. #include <QWidget>
    11. #include <QGraphicsSceneMouseEvent>
    12. #include <QGraphicsSceneHoverEvent>
    13. #include <QMessageBox>
    14. #include <shapelib/shapefil.h>
    15.  
    16. #include <object.h>
    17.  
    18. class item : public QObject, public QGraphicsItem
    19. {
    20. Q_OBJECT
    21. public:
    22. item(QGraphicsItem *parent = 0);
    23.  
    24. QRectF boundingRect() const;
    25. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    26. void OpenShapeFile(char *fileName);
    27.  
    28. virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
    29. virtual void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
    30. virtual void mouseMoveEvent(QGraphicsSceneEvent *event);
    31.  
    32. QVector<object*> vMap;
    33. object *map;
    34.  
    35. private:
    36. typedef struct MyPoint2D
    37. {
    38. double x;
    39. double y;
    40. }MyPoint2D;
    41.  
    42. std::vector<MyPoint2D> vPoints;
    43.  
    44. typedef struct MyLineString2D
    45. {
    46. std::vector<MyPoint2D> vPointList;
    47. }MyLineString2D;
    48.  
    49. //Holds coordinates of Line Shapefile
    50. std::vector<MyLineString2D> vLines;
    51.  
    52. typedef struct MyPolygon2D
    53. {
    54. std::vector<MyPoint2D> vPointList;
    55.  
    56. }MyPolygon2D;
    57.  
    58. //Holds coordinates of Polygon Shapefile
    59. std::vector<MyPolygon2D> vPolygons;
    60.  
    61. typedef struct SBoundingBox
    62. {
    63. qreal maxX;
    64. qreal maxY;
    65. qreal minX;
    66. qreal minY;
    67. }SBoundingBox;
    68.  
    69. qreal xMax,yMax,x2Min,y2Min;
    70. //BoundingBox of Shapefile
    71. SBoundingBox sBoundingBox;
    72. };
    73.  
    74. #endif // ITEM_H
    To copy to clipboard, switch view to plain text mode 

    And the .cpp file

    Qt Code:
    1. #include "item.h"
    2.  
    3. item::item(QGraphicsItem *parent)
    4. : QObject(), QGraphicsItem(parent)
    5. {
    6. setAcceptHoverEvents(true);
    7. setFlags(ItemIsMovable | ItemIsFocusable | QGraphicsItem::ItemIsSelectable);
    8. }
    9.  
    10. QRectF item::boundingRect() const
    11. {
    12. QRectF recWindow;
    13. recWindow.setBottomLeft(QPointF(sBoundingBox.minX,sBoundingBox.minY));
    14. recWindow.setBottomRight(QPointF(sBoundingBox.maxX, sBoundingBox.minY));
    15. recWindow.setTopLeft(QPointF(sBoundingBox.minX,sBoundingBox.maxY));
    16. recWindow.setTopRight(QPointF(sBoundingBox.maxX,sBoundingBox.maxY));
    17. return recWindow;
    18. }
    19.  
    20. void item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    21. {
    22. OpenShapeFile("./Maps/Norden_wgs84.shp");
    23. painter->setRenderHint(QPainter::Antialiasing);
    24. painter->setBrush(Qt::red);
    25.  
    26. for(int i = 0;i< vMap.size(); i++)
    27. {
    28. painter->drawPolyline(vMap.at(i)->p.data() ,vMap.at(i)->p.size());
    29. }
    30. }
    31.  
    32. void item::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
    33. {
    34. }
    35.  
    36. void item::mousePressEvent(QGraphicsSceneMouseEvent* event)
    37. {
    38. }
    39.  
    40. void item::mouseMoveEvent(QGraphicsSceneEvent *event)
    41. {
    42. }
    43.  
    44. void item::OpenShapeFile(char *fileName)
    45. {
    46. SHPHandle hSHP = SHPOpen(fileName, "rb");
    47.  
    48. if(hSHP == NULL)
    49. {
    50. m.setText("Could not open shape.");
    51. m.exec();
    52. }
    53. else
    54. {
    55. //Read boundingbox of shapefile.
    56. sBoundingBox.maxX = hSHP->adBoundsMax[0];
    57. sBoundingBox.maxY = hSHP->adBoundsMax[1];
    58.  
    59. sBoundingBox.minX = hSHP->adBoundsMin[0];
    60. sBoundingBox.minY = hSHP->adBoundsMin[1];
    61.  
    62. //Point shape-file
    63. if(hSHP->nShapeType == SHPT_POINT)
    64. {
    65. SHPObject *psShape;
    66. for(int i=0;i<hSHP->nRecords;i++)
    67. {
    68. psShape = SHPReadObject(hSHP, i);
    69. double fX = psShape->padfX[0];
    70. double fY = -psShape->padfY[0];
    71.  
    72. //Plot these points
    73. MyPoint2D pt;
    74. pt.x = fX;
    75. pt.y = fY;
    76.  
    77. vPoints.push_back(pt);
    78. }
    79. }
    80.  
    81. //Line Shapefile
    82. else if(hSHP->nShapeType == SHPT_ARC)
    83. {
    84. SHPObject *psShape;
    85.  
    86. for(int i=0;i<hSHP->nRecords;i++)
    87. {
    88. psShape = SHPReadObject(hSHP, i);
    89. std::vector<MyPoint2D> tempPointArray;
    90. QVector<QPointF> tempQPointArray;
    91. map = new object();
    92.  
    93. for(int j=0;j<psShape->nVertices;j++)
    94. {
    95. double fX = psShape->padfX[j];
    96. double fY = psShape->padfY[j];
    97. MyPoint2D pt;
    98. pt.x = fX;
    99. pt.y= fY;
    100.  
    101. //QT Point
    102. QPointF test = QPointF(fX,fY);
    103.  
    104. map->addVertices(test);
    105. tempPointArray.push_back(pt);
    106. }
    107.  
    108. vMap.push_back(map);
    109.  
    110. MyLineString2D linestring;
    111. linestring.vPointList=tempPointArray;
    112. vLines.push_back(linestring);
    113. }
    114. }
    115.  
    116. //Polygon Shapefile
    117. if(hSHP->nShapeType == SHPT_POLYGON)
    118. {
    119. SHPObject *psShape;
    120. for(int i=0;i<hSHP->nRecords;i++)
    121. {
    122. psShape = SHPReadObject(hSHP, i);
    123. std::vector<MyPoint2D> tempPointArray;
    124.  
    125. for(int j=0;j<psShape->nVertices;j++)
    126. {
    127. double fX = psShape->padfX[j];
    128. double fY = psShape->padfY[j];
    129. MyPoint2D pt;
    130. pt.x = fX;
    131. pt.y= fY;
    132.  
    133. tempPointArray.push_back(pt);
    134. }
    135.  
    136. MyPolygon2D polygon;
    137. polygon.vPointList = tempPointArray;
    138. vPolygons.push_back(polygon);
    139. }
    140. }
    141. }
    142. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView coordinate system

    Looking your ::boundingRect() method, did you scale view's Y to -1 or not ?
    In QGraphicsScene nominal coordinate system, the bottom of a valid QRectF has a greater value than its top, which is not the case in your code (bottom<top).

    Try to define your boundingrect as follow : a point in space (x,y) and 2D dimensions (width,height)

    Otherwise I don't see what can be wrong for now..

    Beside, you define custom structs where using Qt would just be fine : I'm thinking of

    MyPolygon2D => QPolygonF
    MyPoint2D => QPointF
    MyLineString2D => QPainterPath
    SBoundingBox => QRectF

    Another thing : you read your file and initialize your structs each time you paint your item, which is not good. You could read your file once in constructor, initialize some member and use these members in your paint method. Plus, it seems your OpenShapeFile() contains memory leaks (1 new == 1 delete somewhere in your code)

  11. #11
    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: QGraphicsView coordinate system

    Sorry to butt in like this but if your whole shapefile is represented by a single item then that's probably not a very good approach. As for zooming, what you want is to zoom in the view on the item and not zoom in the view on the scene.

    Something like:
    Qt Code:
    1. ui->graphicsView->setScene(scene);
    2. scene->addItem(test);
    3. ui->graphicsView->fitInView(test->sceneRect(), Qt::KeepAspectRatio);
    To copy to clipboard, switch view to plain text mode 
    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.


  12. #12
    Join Date
    Nov 2007
    Posts
    26
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsView coordinate system

    I have had a similar thing happen (screen going white / disapearing) when my custom items bounding rect has been wrong. Try just adding a normal QGraphicsLineIteminstead to see if your zooming is ok.

    I had good results displaying dwg files by just with a QGraphicsItemGroup and just adding the individual lines, text, path items.

  13. #13
    Join Date
    Sep 2013
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsView coordinate system

    can you please send me the source folder

  14. #14
    Join Date
    Sep 2013
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Lightbulb Re: QGraphicsView coordinate system

    hi all,

    I would try if you share me the total source code folder. other way share object.h file and mainwindow.cpp, mainwindow.h,main.cpp.

    Thanks & regards,

    Swapan

  15. #15
    Join Date
    Sep 2013
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Cool Re: QGraphicsView coordinate system

    if possible then share the source for object.h file

  16. #16
    Join Date
    Sep 2013
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsView coordinate system

    Atlast I solve the problem.........

    swapan

Similar Threads

  1. QGraphicsView coordinate system
    By been_1990 in forum Qt Programming
    Replies: 7
    Last Post: 2nd November 2010, 16:21
  2. coordinate system
    By Wojtek.wk in forum Newbie
    Replies: 7
    Last Post: 12th April 2010, 13:47
  3. 2D Graphics on coordinate system
    By soumya in forum Qt Programming
    Replies: 0
    Last Post: 4th November 2009, 06:27
  4. Replies: 1
    Last Post: 9th April 2009, 14:54
  5. The coordinate system
    By avis_phoenix in forum Qt Programming
    Replies: 1
    Last Post: 28th July 2008, 12:16

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.