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
{
}
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::blue);
for(int i = 0;i< vMap.size(); i++)
{
painter->drawPolyline(vMap.at(i)->p.data() ,vMap.at(i)->p.size());
}
}
item::item(QGraphicsItem *parent)
: QGraphicsItem(parent)
{
}
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::blue);
for(int i = 0;i< vMap.size(); i++)
{
painter->drawPolyline(vMap.at(i)->p.data() ,vMap.at(i)->p.size());
}
}
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
IceChart3
::IceChart3(QWidget *parent
){
ui.setupUi(this);
sceneRect.
setBottomLeft(QPoint(4,
52));
sceneRect.
setBottomRight(QPoint(42,
52));
sceneRect.
setTopLeft(QPoint(4,
72));
sceneRect.
setTopRight(QPoint(42,
72));
scene = new MyScene(this);
scene->setSceneRect(sceneRect);
itemMap = new item();
scene->addItem(itemMap);
//Flip upside down.
ui.graphicsView->scale(1,-1);
//Set scene to view
ui.graphicsView->setScene(scene);
IceChart3::IceChart3(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
QRect sceneRect;
sceneRect.setBottomLeft(QPoint(4,52));
sceneRect.setBottomRight(QPoint(42, 52));
sceneRect.setTopLeft(QPoint(4,72));
sceneRect.setTopRight(QPoint(42,72));
scene = new MyScene(this);
scene->setSceneRect(sceneRect);
itemMap = new item();
scene->addItem(itemMap);
//Flip upside down.
ui.graphicsView->scale(1,-1);
//Set scene to view
ui.graphicsView->setScene(scene);
To copy to clipboard, switch view to plain text mode
Bookmarks