Get updated size of QGraphicsView
In my Qt Application I am dynamically creating QGraphicsView(s) and adding them inside a QGridLayout. When I add first view inside grid, the view covers all the available space inside grid. Then I add second view and 开发者_开发问答there are now two equally sized views inside grid. Then I add third view and there are now three equally sized views inside grid. And so on.
How can I get updated size of first view?
Below is my trial but I think this is not working.
//Definition of viewsPerRow
static const int viewsPerRow = 3;
void window::newViewRequested()
{
QGraphicsView *view = new QGraphicsView;
view->setVisible(true);
viewVector.push_back(view);
for(int i = viewGrid->count(); i < viewVector.count(); i++)
{
viewGrid->addWidget(view,i / viewsPerRow ,i % viewsPerRow);
}
qDebug()<<viewGrid->cellRect(0,0);
}
You probably should use the QWidget::rect()
methods to get the geometry of the views themselves. Here is a sample that should work as intended.
QVector< QGraphicsView* > views;
for (int i=0 ; i < 3; ++i) {
QGraphicsView *view = new QGraphicsView;
view->setVisible(true);
viewGrid->addWidget(view, i ,0);
views.push_back(view);
}
qDebug() << "The view size of the first view is " << views[0]->rect();
Not sure why you need the size of the first view, but Qt does only few redraws at the time it "thinks" it is suited. Therefore you can't rely on a size you query outside the view.
I'd try to use the resizeEvent to get notified when the size of an item changes and perform the necessary actions then.
精彩评论