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>
public:
MainWindow();
void updateLogo();
private:
protected:
};
#include <QtGui>
class MainWindow: public QMainWindow{
public:
MainWindow();
void updateLogo();
private:
QWidget* centralw;
protected:
void resizeEvent(QResizeEvent* event);
};
To copy to clipboard, switch view to plain text mode
mainwindow.cpp:
#include "mainwindow.h"
#include <iostream>
using namespace std;
MainWindow::MainWindow()
{
menuBar()->addMenu(tr("File"));
setCentralWidget(centralw);
}
{
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;
}
#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;
}
To copy to clipboard, switch view to plain text mode
main.cpp:
#include <QtGui>
#include "mainwindow.h"
int main(int argc, char* argv[])
{
MainWindow mw;
mw.show();
mw.updateLogo();
return app.exec();
}
#include <QtGui>
#include "mainwindow.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
MainWindow mw;
mw.show();
mw.updateLogo();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks