Results 1 to 12 of 12

Thread: How to change QMainWindow size at runtime?

  1. #1
    Join Date
    Jan 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default How to change QMainWindow size at runtime?

    Since there was no chance to determine the size of main window at very beginning, I have to change it later, at runtime.

    Basically my main window contained a toolbar, menu bar, as well as status bar. Then
    when the first child widget (will be central widget of main window) was created, it's best chance for me to adjust the size.

    The code liked below,

    Qt Code:
    1. // first set child widget geometry
    2. setGeometry(rect);
    3.  
    4. // adjust main window accordingly
    5. mainWnd->setCentralWidget(this);
    6. mainWnd->adjustSize();
    7.  
    8. // I also tried below
    9. // mainWnd->updateGeometry();
    To copy to clipboard, switch view to plain text mode 
    Besides, I've overridden sizeHint() of main window to return a QSize(0, 0), since it said if sizeHint() returned an invalid value, adjustSize() would set the size to the children rectangle that covers all child widgets.

    However, the resulting size of main window was totally too small than I expected.
    I'm really confused how Qt size policy was working.

    Could anyone help me?
    Thx.
    Last edited by high_flyer; 6th January 2011 at 09:35. Reason: code tags

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to change QMainWindow size at runtime?

    I am not sure I understand what it is you want to achieve, and more so why.
    Have you tried resize()?

    Also, if your main window needs to have a certain specific size, then you can have sizeHint() return it, in addition, if your main window should not change size you should set the sitzePolicy() to fixed.
    But fixed sized main window (or windows that need to have more specific size than using the min and max size limits) shows more that you probably don't need a main window, and probably more a dialog.

    Maybe if you explain more what it is and why you are trying to do, we might help you get on the right track.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to change QMainWindow size at runtime?

    Basically, I'd like to build an app with main window by following steps,

    1. build main window (size was not determined yet at this moment). now, main window only contained toolbar, menu, status bar, without a central widget
    2. then dynamically create the first widget (QFrame-derived), it would be central widget of main window and it would expand main window size according to its size
    3. continue to dynamically create some widgets and add them to central widget.

    Briefly, main window size depends on central widget size.

    Since main window size = all bars size + central widget size,
    so resize() didn't work for my case, it actually resized entire geometry area which contains those bars, but I've no idea about what those bars size are.

    Hopefully I'm clear for you.
    Thanks.
    Last edited by wysota; 6th January 2011 at 11:25.

  4. #4
    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: How to change QMainWindow size at runtime?

    First make sure your central widget is using layouts properly. Then put it into the main window (if you really need a main window) and set the main window layout's size constraint (not size policy of any of the widgets) to QLayout::SetFixedSize (or SetMinimumSize).
    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.


  5. #5
    Join Date
    Jan 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to change QMainWindow size at runtime?

    I built a simple demo to show what problem I've got,

    Qt Code:
    1. QMyMainWindow::QMyMainWindow(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags)
    2. {
    3. setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    4.  
    5. QFrame* frame = new QFrame(this);
    6. setCentralWidget(frame);
    7.  
    8. frame->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    9.  
    10. QRect rect(0, 0, 240, 320);
    11. frame->setFrameShape(QFrame::Box);
    12. frame->setLineWidth(3);
    13. frame->setFrameShadow(QFrame::Plain);
    14. frame->setGeometry(rect);
    15.  
    16. adjustSize();
    17. }
    To copy to clipboard, switch view to plain text mode 

    I didn't specify main window size at first and it would be totally expanded by central widget.
    I also tried other SizePolicy values, but the resulting size of main window was not (240, 320) as expected at all.

    How could I do?
    Thx
    Last edited by myfifth; 7th January 2011 at 06:44.

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to change QMainWindow size at runtime?

    You have to resize() your mainwindow, not the frame inside it. And calling adjustSize() destroy all other setting you made before.

  7. #7
    Join Date
    Jan 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to change QMainWindow size at runtime?

    I've found the root cause

    Qt Code:
    1. QSize QWidgetPrivate::adjustedSize() const
    2. {
    3. Q_Q(const QWidget);
    4.  
    5. QSize s = q->sizeHint();
    6.  
    7. if (q->isWindow()) {
    8. Qt::Orientations exp;
    9. if (layout) {
    10. if (layout->hasHeightForWidth())
    11. s.setHeight(layout->totalHeightForWidth(s.width()));
    12. exp = layout->expandingDirections();
    13. } else
    14. {
    15. if (q->sizePolicy().hasHeightForWidth())
    16. s.setHeight(q->heightForWidth(s.width()));
    17. exp = q->sizePolicy().expandingDirections();
    18. }
    19. if (exp & Qt::Horizontal)
    20. s.setWidth(qMax(s.width(), 200));
    21. if (exp & Qt::Vertical)
    22. s.setHeight(qMax(s.height(), 100));
    23. #if defined(Q_WS_X11)
    24. QRect screen = QApplication::desktop()->screenGeometry(q->x11Info().screen());
    25. #else // all others
    26. QRect screen = QApplication::desktop()->screenGeometry(q->pos());
    27. #endif
    28. #if defined (Q_WS_WINCE) || defined (Q_OS_SYMBIAN)
    29. s.setWidth(qMin(s.width(), screen.width()));
    30. s.setHeight(qMin(s.height(), screen.height()));
    31. #else
    32. s.setWidth(qMin(s.width(), screen.width()*2/3));
    33. s.setHeight(qMin(s.height(), screen.height()*2/3));
    34. #endif
    35. if (QTLWExtra *extra = maybeTopData())
    36. extra->sizeAdjusted = true;
    37. }
    38.  
    39. if (!s.isValid()) {
    40. QRect r = q->childrenRect(); // get children rectangle
    41. if (r.isNull())
    42. return s;
    43. s = r.size() + QSize(2 * r.x(), 2 * r.y());
    44. }
    45.  
    46. return s;
    47. }
    To copy to clipboard, switch view to plain text mode 

    The root was that window has a default size policy, so adjustSize() never ran into using childrenRect to expand size. How could I avoid to it?


    Added after 4 minutes:


    Quote Originally Posted by Lykurg View Post
    You have to resize() your mainwindow, not the frame inside it. And calling adjustSize() destroy all other setting you made before.
    but I can't get the total size of frame plus other bars (toolbar, menubar, etc).
    Last edited by myfifth; 7th January 2011 at 09:22.

  8. #8
    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: How to change QMainWindow size at runtime?

    size policy is only relevant for widgets inside layouts. Your main window is not inside a layout. Furthermore your frame doesn't have a layout attached so it's sizeHint is practically 0. Hence the size of your mainwindow content is also 0. Why do you keep going the wrong way despite being told a couple of times how you should approach your problem? childrenRect() will likely use sizeHint() of your child widgets and since sizeHint of the frame is 0, you will get a useless output for the whole calculation. And even if not then your frame is controlled by a layout so using setGeometry() on it has no effect at all.
    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.


  9. #9
    Join Date
    Jan 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to change QMainWindow size at runtime?

    Quote Originally Posted by wysota View Post
    First make sure your central widget is using layouts properly. Then put it into the main window (if you really need a main window) and set the main window layout's size constraint (not size policy of any of the widgets) to QLayout::SetFixedSize (or SetMinimumSize).
    What layout should be assinged to central frame?

    Quote Originally Posted by wysota View Post
    size policy is only relevant for widgets inside layouts. Your main window is not inside a layout. Furthermore your frame doesn't have a layout attached so it's sizeHint is practically 0. Hence the size of your mainwindow content is also 0. Why do you keep going the wrong way despite being told a couple of times how you should approach your problem? childrenRect() will likely use sizeHint() of your child widgets and since sizeHint of the frame is 0, you will get a useless output for the whole calculation. And even if not then your frame is controlled by a layout so using setGeometry() on it has no effect at all.
    As far as I known, QMainWindow has its default layout management, why did you say that it's not inside a layout?

    I didn't quite follow your words. I'm really confused with concept layout in Qt, and things like relationship between size policy and layout
    Could you please explain it little more or refer me to something to read?
    Thanks.

  10. #10
    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: How to change QMainWindow size at runtime?

    Quote Originally Posted by myfifth View Post
    What layout should be assinged to central frame?
    It depends on the contents of the frame. If it has no contents then layout is not required but you need to manually fix the size of the frame to something constant using QWidget::setFixedSize.

    As far as I known, QMainWindow has its default layout management, why did you say that it's not inside a layout?
    A layout of a widget is inside the widget not outside it. The main window is a floating window so it's not "in" a layout (as it would need to have a parent).

    Could you please explain it little more or refer me to something to read?
    Size policy is a set of rules that are used by a layout controlling the current widget to determine how much space to give to each widget controlled by the layout. Layout controlls the widgets that are in the layout, not the one the layout is in (the parent). If a particular widget (like your main window) is not part of some layout, its size policy and its size hint are ignored. The exception is that size hint of a window is used to determine the initial size of this window.
    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.


  11. #11
    Join Date
    Jan 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to change QMainWindow size at runtime?

    @wysota

    I'm now much more clear.
    Layout in a widget is used to control layouting of its children, and size policy defines how layout works.

    In my case, I should care about how to design frame layout instead of main window. I'll try it later.

    Thanks a lot.

  12. #12
    Join Date
    Jan 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to change QMainWindow size at runtime?

    @wysota
    Sorry for late reply.
    setFixedSize and setSizeConstraint worked quite well. I simply set both of them to expected values, finally it rocked.

    Really thx.

Similar Threads

  1. QToolbar - change icon at runtime ?
    By pl01 in forum Qt Programming
    Replies: 6
    Last Post: 18th November 2010, 17:51
  2. Change QPixmap image at runtime
    By Qt Coder in forum Qt Programming
    Replies: 12
    Last Post: 30th March 2009, 12:37
  3. Replies: 2
    Last Post: 23rd March 2009, 17:26
  4. how change language at runtime?
    By mattia in forum Newbie
    Replies: 3
    Last Post: 14th November 2007, 18:36
  5. change font size and button size of QMessageBox
    By nass in forum Qt Programming
    Replies: 6
    Last Post: 13th September 2006, 19:16

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.