Results 1 to 3 of 3

Thread: QMainWindow -> centralWidget size

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

    Default QMainWindow -> centralWidget size

    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-p...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-t...ntry&id=237451
    Could you suggest a workaround?
    Last edited by lalesculiviu; 24th October 2009 at 15:47. Reason: updated contents

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QMainWindow -> centralWidget size

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    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 10:50.

Similar Threads

  1. Replies: 2
    Last Post: 23rd March 2009, 18:26
  2. Maximumsize by layout QMainWindow
    By captiva in forum Qt Programming
    Replies: 7
    Last Post: 24th February 2009, 20:41
  3. Shrink QMainWindow to Minimum Size
    By kloffy in forum Qt Programming
    Replies: 4
    Last Post: 17th February 2008, 18:54
  4. QLabel size policy
    By Caius Aérobus in forum Qt Programming
    Replies: 3
    Last Post: 7th December 2007, 18:57
  5. QMainWindow: problem changing centralWidget
    By Caius Aérobus in forum Qt Programming
    Replies: 6
    Last Post: 4th October 2007, 14: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.