Results 1 to 16 of 16

Thread: QGraphicsView coordinate system

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  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

    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

  3. #3
    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 

  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

    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)

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
  •  
Qt is a trademark of The Qt Company.