PDA

View Full Version : Real size in pixels of a widget



yosefm
27th October 2007, 14:52
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.

jpn
27th October 2007, 15:45
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?

yosefm
27th October 2007, 16:00
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 :-)

momesana
27th October 2007, 16:01
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.



#include <QApplication>
#include <QtGui>

class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget* parent=0) : QMainWindow(parent) {
view = new QGraphicsView;
view->installEventFilter(this);
setCentralWidget(view);

}
bool eventFilter(QObject* obj, QEvent* event) {
if (obj == view && event->type() == QEvent::Resize)
statusBar()->showMessage(QString::number(view->width()));
return QMainWindow::eventFilter(obj, event);
}
private:
QGraphicsView* view;


};

#include "main.moc"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
MainWindow mw;
mw.show();
return app.exec();
}