Results 1 to 3 of 3

Thread: QMainWindow -> centralWidget size

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2009
    Location
    Craiova, Romania
    Posts
    46
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    3

    Default Re: QMainWindow -> centralWidget size

    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:

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class MainWindow: public QMainWindow{
    4. public:
    5. MainWindow();
    6. void updateLogo();
    7.  
    8. private:
    9. QWidget* centralw;
    10.  
    11. protected:
    12. void resizeEvent(QResizeEvent* event);
    13. };
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp:

    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. #include <iostream>
    4. using namespace std;
    5.  
    6. MainWindow::MainWindow()
    7. {
    8. menuBar()->addMenu(tr("File"));
    9.  
    10. centralw=new QWidget();
    11. setCentralWidget(centralw);
    12. }
    13.  
    14. void MainWindow::resizeEvent(QResizeEvent* event)
    15. {
    16. QMainWindow::resizeEvent(event);
    17.  
    18. updateLogo();
    19. }
    20.  
    21. void MainWindow::updateLogo()
    22. {
    23. if(centralWidget())
    24. cout<<"central widget height=="<<centralWidget()->size().height()<<endl;
    25. cout<<"menubar size height=="<<menuBar()->size().height()<<endl;
    26. cout<<"menubar sizehint height=="<<menuBar()->sizeHint().height()<<endl;
    27. cout<<endl;
    28. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp:

    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "mainwindow.h"
    4.  
    5. int main(int argc, char* argv[])
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. MainWindow mw;
    10.  
    11. mw.show();
    12.  
    13. mw.updateLogo();
    14.  
    15. return app.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files
    Last edited by lalesculiviu; 25th October 2009 at 09:50.

Similar Threads

  1. Replies: 2
    Last Post: 23rd March 2009, 17:26
  2. Maximumsize by layout QMainWindow
    By captiva in forum Qt Programming
    Replies: 7
    Last Post: 24th February 2009, 19:41
  3. Shrink QMainWindow to Minimum Size
    By kloffy in forum Qt Programming
    Replies: 4
    Last Post: 17th February 2008, 17:54
  4. QLabel size policy
    By Caius Aérobus in forum Qt Programming
    Replies: 3
    Last Post: 7th December 2007, 17:57
  5. QMainWindow: problem changing centralWidget
    By Caius Aérobus in forum Qt Programming
    Replies: 6
    Last Post: 4th October 2007, 13:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.