PDA

View Full Version : QMainWindow -> centralWidget size



lalesculiviu
24th October 2009, 13:37
Hello! I am trying to make a scaled drawing on a main form. For this, I took the centralWidget width() and height() and compute the dimensions of the drawing. But I have a problem:

Say the initial size of central widget is 400x300. After I show the QMainWindow, the size is the same. But if I manually resize the QMainWindow with just 1 pixel, or just modify width, the height of the central widget automatically decreases by 21 (so it is now 400x279).

What am I doing wrong?

Edited to add more details: I am using a designer ui file, a QMainWindow descendant, with a menu implemented in the designer. When I get the geometry of the central widget, x is 0 and y is 2 (so it may be wrong the y? Because the menu bar has a height of about 20). After I manually resize the main form, y becomes 23 (and x stays 0). So, the geometry of the central widget before the manual resize seems wrong.

Also, if I write in code: main form resize(30,30) and resize(old x,old y), the geometry of the central widget is correct now (x=0, y=23).

Edited to add: of course I reimplemented the resizeEvent to update my resizable drawing (I draw a few lines, taking into account the available size of central widget).

Edited to add: I found this link: http://www.qtcentre.org/forum/f-qt-programming-2/t-qgraphicsview-and-qgraphicsscene-17358-post86938.html
The person says it is a Qt bug, because the menu bar is reporting height = 2 incorrectly, but how can I find when it will be solved?

Edited to add: the bug is on: http://qt.nokia.com/developer/task-tracker/index_html?method=entry&id=237451
Could you suggest a workaround?

wysota
25th October 2009, 08:59
When do you retrieve the size of the central widget? Geometry of widgets is set when they are first shown so if you are doing it before showEvent() is called, you'll get bogus values - maybe that's what you are suffering from.

lalesculiviu
25th October 2009, 09:40
I attach my code, also I write it here.

You can see height of menu=2, even after show. If you resize the form a bit (manually or even in the code), the height becomes 23.

I added a hack for my program: I keep track of size hint of menuBar and subtract sizeHint().height-size.height() from centralWidget height.

You can see size is wrong even after in main.cpp I call "updateLogo" after show of MainWindow.

Quote from Qt doc for class QMenu method exec():


When positioning a menu with exec() or popup(), bear in mind that you cannot rely on the menu's current size(). For performance reasons, the menu adapts its size only when necessary. So in many cases, the size before and after the show is different. Instead, use sizeHint() which calculates the proper size depending on the menu's current contents.


Listing of small sample:

mainwindow.h:



#include <QtGui>

class MainWindow: public QMainWindow{
public:
MainWindow();
void updateLogo();

private:
QWidget* centralw;

protected:
void resizeEvent(QResizeEvent* event);
};


mainwindow.cpp:



#include "mainwindow.h"

#include <iostream>
using namespace std;

MainWindow::MainWindow()
{
menuBar()->addMenu(tr("File"));

centralw=new QWidget();
setCentralWidget(centralw);
}

void MainWindow::resizeEvent(QResizeEvent* event)
{
QMainWindow::resizeEvent(event);

updateLogo();
}

void MainWindow::updateLogo()
{
if(centralWidget())
cout<<"central widget height=="<<centralWidget()->size().height()<<endl;
cout<<"menubar size height=="<<menuBar()->size().height()<<endl;
cout<<"menubar sizehint height=="<<menuBar()->sizeHint().height()<<endl;
cout<<endl;
}


main.cpp:



#include <QtGui>

#include "mainwindow.h"

int main(int argc, char* argv[])
{
QApplication app(argc, argv);

MainWindow mw;

mw.show();

mw.updateLogo();

return app.exec();
}