views:

187

answers:

2

Hi,

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);
    }
A: 

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();
Lohrun
@Lohrun I tried your code. When I added first view I got default size of the QGraphicsView which is 256 x 192. When I added the second view I got calculated size but this time calculation was wrong(I think it considers there exist only one view). When I added the third one I got wrong calculated size again(This time I think it considers there exists two views)
onurozcelik
A: 

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.

gre