Hmm ok maybe like you said I´m doing something wrong in with the QGraphicsItem.
Here is the .h file
#ifndef ITEM_H
#define ITEM_H
#include <QGraphicsItem>
#include <QPointF>
#include <QRectF>
#include <QPolygonF>
#include <QPainter>
#include <QPainterPath>
#include <QWidget>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsSceneHoverEvent>
#include <QMessageBox>
#include <shapelib/shapefil.h>
#include <object.h>
{
Q_OBJECT
public:
void OpenShapeFile(char *fileName);
QVector<object*> vMap;
object *map;
private:
typedef struct MyPoint2D
{
double x;
double y;
}MyPoint2D;
std::vector<MyPoint2D> vPoints;
typedef struct MyLineString2D
{
std::vector<MyPoint2D> vPointList;
}MyLineString2D;
//Holds coordinates of Line Shapefile
std::vector<MyLineString2D> vLines;
typedef struct MyPolygon2D
{
std::vector<MyPoint2D> vPointList;
}MyPolygon2D;
//Holds coordinates of Polygon Shapefile
std::vector<MyPolygon2D> vPolygons;
typedef struct SBoundingBox
{
qreal maxX;
qreal maxY;
qreal minX;
qreal minY;
}SBoundingBox;
qreal xMax,yMax,x2Min,y2Min;
//BoundingBox of Shapefile
SBoundingBox sBoundingBox;
};
#endif // ITEM_H
#ifndef ITEM_H
#define ITEM_H
#include <QGraphicsItem>
#include <QPointF>
#include <QRectF>
#include <QPolygonF>
#include <QPainter>
#include <QPainterPath>
#include <QWidget>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsSceneHoverEvent>
#include <QMessageBox>
#include <shapelib/shapefil.h>
#include <object.h>
class item : public QObject, public QGraphicsItem
{
Q_OBJECT
public:
item(QGraphicsItem *parent = 0);
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void OpenShapeFile(char *fileName);
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
virtual void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
virtual void mouseMoveEvent(QGraphicsSceneEvent *event);
QVector<object*> vMap;
object *map;
private:
typedef struct MyPoint2D
{
double x;
double y;
}MyPoint2D;
std::vector<MyPoint2D> vPoints;
typedef struct MyLineString2D
{
std::vector<MyPoint2D> vPointList;
}MyLineString2D;
//Holds coordinates of Line Shapefile
std::vector<MyLineString2D> vLines;
typedef struct MyPolygon2D
{
std::vector<MyPoint2D> vPointList;
}MyPolygon2D;
//Holds coordinates of Polygon Shapefile
std::vector<MyPolygon2D> vPolygons;
typedef struct SBoundingBox
{
qreal maxX;
qreal maxY;
qreal minX;
qreal minY;
}SBoundingBox;
qreal xMax,yMax,x2Min,y2Min;
//BoundingBox of Shapefile
SBoundingBox sBoundingBox;
};
#endif // ITEM_H
To copy to clipboard, switch view to plain text mode
And the .cpp file
#include "item.h"
{
setAcceptHoverEvents(true);
setFlags
(ItemIsMovable | ItemIsFocusable |
QGraphicsItem::ItemIsSelectable);
}
QRectF item
::boundingRect() const {
recWindow.
setBottomLeft(QPointF(sBoundingBox.
minX,sBoundingBox.
minY));
recWindow.
setBottomRight(QPointF(sBoundingBox.
maxX, sBoundingBox.
minY));
recWindow.
setTopLeft(QPointF(sBoundingBox.
minX,sBoundingBox.
maxY));
recWindow.
setTopRight(QPointF(sBoundingBox.
maxX,sBoundingBox.
maxY));
return recWindow;
}
{
OpenShapeFile("./Maps/Norden_wgs84.shp");
painter
->setRenderHint
(QPainter::Antialiasing);
painter->setBrush(Qt::red);
for(int i = 0;i< vMap.size(); i++)
{
painter->drawPolyline(vMap.at(i)->p.data() ,vMap.at(i)->p.size());
}
}
{
}
{
}
{
}
void item::OpenShapeFile(char *fileName)
{
SHPHandle hSHP = SHPOpen(fileName, "rb");
if(hSHP == NULL)
{
m.setText("Could not open shape.");
m.exec();
}
else
{
//Read boundingbox of shapefile.
sBoundingBox.maxX = hSHP->adBoundsMax[0];
sBoundingBox.maxY = hSHP->adBoundsMax[1];
sBoundingBox.minX = hSHP->adBoundsMin[0];
sBoundingBox.minY = hSHP->adBoundsMin[1];
//Point shape-file
if(hSHP->nShapeType == SHPT_POINT)
{
SHPObject *psShape;
for(int i=0;i<hSHP->nRecords;i++)
{
psShape = SHPReadObject(hSHP, i);
double fX = psShape->padfX[0];
double fY = -psShape->padfY[0];
//Plot these points
MyPoint2D pt;
pt.x = fX;
pt.y = fY;
vPoints.push_back(pt);
}
}
//Line Shapefile
else if(hSHP->nShapeType == SHPT_ARC)
{
SHPObject *psShape;
for(int i=0;i<hSHP->nRecords;i++)
{
psShape = SHPReadObject(hSHP, i);
std::vector<MyPoint2D> tempPointArray;
QVector<QPointF> tempQPointArray;
map = new object();
for(int j=0;j<psShape->nVertices;j++)
{
double fX = psShape->padfX[j];
double fY = psShape->padfY[j];
MyPoint2D pt;
pt.x = fX;
pt.y= fY;
//QT Point
map->addVertices(test);
tempPointArray.push_back(pt);
}
vMap.push_back(map);
MyLineString2D linestring;
linestring.vPointList=tempPointArray;
vLines.push_back(linestring);
}
}
//Polygon Shapefile
if(hSHP->nShapeType == SHPT_POLYGON)
{
SHPObject *psShape;
for(int i=0;i<hSHP->nRecords;i++)
{
psShape = SHPReadObject(hSHP, i);
std::vector<MyPoint2D> tempPointArray;
for(int j=0;j<psShape->nVertices;j++)
{
double fX = psShape->padfX[j];
double fY = psShape->padfY[j];
MyPoint2D pt;
pt.x = fX;
pt.y= fY;
tempPointArray.push_back(pt);
}
MyPolygon2D polygon;
polygon.vPointList = tempPointArray;
vPolygons.push_back(polygon);
}
}
}
}
#include "item.h"
item::item(QGraphicsItem *parent)
: QObject(), QGraphicsItem(parent)
{
setAcceptHoverEvents(true);
setFlags(ItemIsMovable | ItemIsFocusable | QGraphicsItem::ItemIsSelectable);
}
QRectF item::boundingRect() const
{
QRectF recWindow;
recWindow.setBottomLeft(QPointF(sBoundingBox.minX,sBoundingBox.minY));
recWindow.setBottomRight(QPointF(sBoundingBox.maxX, sBoundingBox.minY));
recWindow.setTopLeft(QPointF(sBoundingBox.minX,sBoundingBox.maxY));
recWindow.setTopRight(QPointF(sBoundingBox.maxX,sBoundingBox.maxY));
return recWindow;
}
void item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
OpenShapeFile("./Maps/Norden_wgs84.shp");
painter->setRenderHint(QPainter::Antialiasing);
painter->setBrush(Qt::red);
for(int i = 0;i< vMap.size(); i++)
{
painter->drawPolyline(vMap.at(i)->p.data() ,vMap.at(i)->p.size());
}
}
void item::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
{
}
void item::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
}
void item::mouseMoveEvent(QGraphicsSceneEvent *event)
{
}
void item::OpenShapeFile(char *fileName)
{
SHPHandle hSHP = SHPOpen(fileName, "rb");
if(hSHP == NULL)
{
QMessageBox m;
m.setText("Could not open shape.");
m.exec();
}
else
{
//Read boundingbox of shapefile.
sBoundingBox.maxX = hSHP->adBoundsMax[0];
sBoundingBox.maxY = hSHP->adBoundsMax[1];
sBoundingBox.minX = hSHP->adBoundsMin[0];
sBoundingBox.minY = hSHP->adBoundsMin[1];
//Point shape-file
if(hSHP->nShapeType == SHPT_POINT)
{
SHPObject *psShape;
for(int i=0;i<hSHP->nRecords;i++)
{
psShape = SHPReadObject(hSHP, i);
double fX = psShape->padfX[0];
double fY = -psShape->padfY[0];
//Plot these points
MyPoint2D pt;
pt.x = fX;
pt.y = fY;
vPoints.push_back(pt);
}
}
//Line Shapefile
else if(hSHP->nShapeType == SHPT_ARC)
{
SHPObject *psShape;
for(int i=0;i<hSHP->nRecords;i++)
{
psShape = SHPReadObject(hSHP, i);
std::vector<MyPoint2D> tempPointArray;
QVector<QPointF> tempQPointArray;
map = new object();
for(int j=0;j<psShape->nVertices;j++)
{
double fX = psShape->padfX[j];
double fY = psShape->padfY[j];
MyPoint2D pt;
pt.x = fX;
pt.y= fY;
//QT Point
QPointF test = QPointF(fX,fY);
map->addVertices(test);
tempPointArray.push_back(pt);
}
vMap.push_back(map);
MyLineString2D linestring;
linestring.vPointList=tempPointArray;
vLines.push_back(linestring);
}
}
//Polygon Shapefile
if(hSHP->nShapeType == SHPT_POLYGON)
{
SHPObject *psShape;
for(int i=0;i<hSHP->nRecords;i++)
{
psShape = SHPReadObject(hSHP, i);
std::vector<MyPoint2D> tempPointArray;
for(int j=0;j<psShape->nVertices;j++)
{
double fX = psShape->padfX[j];
double fY = psShape->padfY[j];
MyPoint2D pt;
pt.x = fX;
pt.y= fY;
tempPointArray.push_back(pt);
}
MyPolygon2D polygon;
polygon.vPointList = tempPointArray;
vPolygons.push_back(polygon);
}
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks