Reputation: 1869
I'm trying to draw a stacked bar graph on Qt, i followed the tutorial on the documentation but when i try to put the graph inside a QGraphicsView
i get a lo of unused space and can't manage to make the scene fit the view.
My code is the same as the documentation one plus the following to make the scene show up in the view:
QGraphicsScene *scene = new QGraphicsScene(this);
scene->addWidget(chartView);
ui->view->setScene(scene);
And what i get is this
As you can see there is a lot of unused space and it makes the text disappear, i wanted to make the graph fit the view but i can't seem to find a way to do so.
I tried in many different ways using
ui->view->ensureVisible ( scene->sceneRect() );
ui->view->fitInView( scene->sceneRect(),Qt::KeepAspectRatio);
and
ui->view->setSceneRect(0,0,ui->view->frameSize().width(),ui->view->frameSize().height());
but nothing seems to work (the second solution just moves the graph to the top left)
Upvotes: 2
Views: 1356
Reputation: 12879
As per the comment... the real issue is the basic sizing of chartView
rather than anything to do with QGraphicsView
or QGraphicsScene
.
When a QWidget
is added to a QGraphicsScene
the resulting QGraphicsProxyWidget
will generally honour the size hint and policy of that widget.
In addition, though, the QGraphicsScene
will set the scene rect to the bounding rectangle of all scene items and the QGraphicsView
will then position the scene according to whatever viewport anchor is in force. The end result can be visually misleading if the scene has a complex set of items or has a bounding rectangle smaller than that displayed within the GraphicsView
.
So if a widget does look odd when added to a QGraphicsScene
it's normally a good idea to test by just showing it as a top level window on the desktop and make sure it behaves as expected before going any further.
Upvotes: 1