Real size in pixels of a widget
Hello all,
I have a QGraphicsView on my main window, whose geometry is reported in QtDesigner to have a width of 579 pixels. However, when I ask for its width in code:
win.mapView.width()
- or -
win.mapView.geometry().width()
I get the value 100. Why is that, and how can I get the real value?
Thanks.
Re: Real size in pixels of a widget
Hi. The size of a widget is calculated when shown for the first time. Before that you will get bogus values unless the size has been explicitly set. Could this be the case for you? Are you asking for width() for example in a constructor?
Re: Real size in pixels of a widget
Yes, that's it. calling win.show() before using width() solves the problem. Boy, it's been a long time since I've done GUI programming :-)
Re: Real size in pixels of a widget
This simple app calculates the width of a QGraphicsView and displays it in a statusbar and updates the value every time the size changes. Check against your own code to see what's wrong.
Code:
#include <QApplication>
#include <QtGui>
{
Q_OBJECT
public:
view->installEventFilter(this);
setCentralWidget(view);
}
if (obj
== view
&& event
->type
() == QEvent::Resize) statusBar
()->showMessage
(QString::number(view
->width
()));
}
private:
};
#include "main.moc"
int main(int argc, char* argv[])
{
MainWindow mw;
mw.show();
return app.exec();
}