QT zoom to Polygon

243 views Asked by At

i have a fixed size QGraphicsview and my own class QGraphWidget class

GraphWidget::GraphWidget(QWidget *parent)
    :QGraphicsView(parent)
{
    QGraphicsScene *scene = new QGraphicsScene(this);
    setScene(scene);

    scale(1,-1);
    setWindowTitle(tr("Poly"));
}


void GraphWidget::showPoly(){
    scene()->clear();
    scene()->setSceneRect(QRectF());
    QPolygonF polygon;
    QPen pen(Qt::black,1);
    QBrush brush(Qt::black);
    brush.setStyle(Qt::SolidPattern);

    polygon<< QPointF(0,0)<< QPointF(10,0)<< QPointF(15,20)<<QPointF(5,10);
    ;
    QGraphicsPolygonItem *polyItem=scene()->addPolygon(polygon,pen);
    fitInView(polyItem);
}

It is possible that the scene focus the polygon and zoom it ? I tried fitinView() or setSceneRect() but nothing worked, the polygon is still very small.


EDIT

fitInView(polyItem->boundingRect());

with fitInView(polyItem->boundingRect());

The problem is that my QGraphicsview is fixed, so the fit in view has to zoom in. It can't change the size of the Qgraphicsview

1

There are 1 answers

0
Hunk On BEST ANSWER

So i got the answer.

The problem was that i called showPoly() inside of the constructor. But on this moment the size of the Qgraphicsview was not fixed.

I use a work around with QSingleShot

#include "mainwindow.h"
#include <QApplication>
#include <QTimer>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    QTimer::singleShot(1, &w,SLOT(callShowPoly()));

    return a.exec();
}