PDA

View Full Version : Understanding GraphicsView Viewport Size



vgrunert
14th April 2015, 16:37
Hi,

I know that I found a similar topic somewhere this for ex. (http://forum.qt.io/topic/4979/setgeometry-weirdness/2) but it does not help me.
Anyway, I really can't figure out why the graphicsview always returns QSize(98,28) as it's size, geometry and viewport size upon construction.

It's a problem because I intend to draw something on the scene based on the actual viewport coordinates.
This of course is not possible since I get the wrong viewport size results.

Here is a small snippet of what I did and would like to do:




//inside a QMainWindow constuctor:
Mainwindow::Mainwindow(QWidget *parent)
: QMainWindow(parent)

{
...
QGraphicsScene *scene = new QGraphicsScene(this);
QGraphicsView *view = new QGraphicsView(this);
view->setScene(scene);
view->show();
drawSomething();
qDebug() << view->viewport()->size();
qDebug() << view->geometry();
...
}

...
void Mainwindow::drawSomething()
{
//draw 5 lines equally spaced aroud the width of the viewport
//does not work because I get the 98 pixels as the width of the viewport width
qreal intervall = view->viewport()->width() / 5;
for(int i = 0; i < 5; ++i)
{
scene->addLine(intervall*i, 0, intervall*i, 1);
}
}

// returns
QSize(98, 28)
QRect(0,0 100x30)


These values are not the actual values of the Graphicsview of course. Now the problem is that I need to draw lines for ex.
based on the actual viewport size.

Many thanks.

wysota
14th April 2015, 17:01
Anyway, I really can't figure out why the graphicsview always returns QSize(98,28) as it's size, geometry and viewport size upon construction.
Widgets have bogus sizes until they are shown for the first time (simply calling show() does not make them visible).


It's a problem because I intend to draw something on the scene based on the actual viewport coordinates.

That rarely makes sense. Are you sure you don't want to draw it as the view's background?

vgrunert
14th April 2015, 17:09
Thank you for the quick reply. No, in fact I am not sure.
But I want the lines to be scaled along with changes in resize events.
Hence I thought that the right approach would be to calculate the lines expressed in their viewport coordinates.


Btw



...
view->setVisible(true);
...


does not work either.

So how do I draw the view at it's construction?

wysota
14th April 2015, 17:32
Thank you for the quick reply. No, in fact I am not sure.
But I want the lines to be scaled along with changes in resize events.
Hence I thought that the right approach would be to calculate the lines expressed in their viewport coordinates.
If you want something to be in viewport coordinates then it doesn't seem to belong in the scene. In my opinion you should implement it in QGraphicsView::drawBackground() reimplementation.



Btw



...
view->setVisible(true);
...


does not work either.
because that's not the point. show() only says "I want to be visible". The actual magic happens when the application processes that event.


So how do I draw the view at its construction?
You don't.

vgrunert
14th April 2015, 17:46
Thank you again. Yes that looks about right. I'll give it a try and post my results once done.

bw

d_stranz
15th April 2015, 17:14
If you want to get the actual size of a widget, you can override the showEvent(). By the time it gets there, the initial size and layout has been calculated and dimensions are correct. If you want to respond to changes in size, you also need to handle resizeEvent(). But beware, resizeEvent() can be called before showEvent(), so you have to wait until showEvent() to ensure that resizeEvent() contains correct widget dimensions.