Results 1 to 5 of 5

Thread: Shrink QMainWindow to Minimum Size

  1. #1
    Join Date
    Aug 2007
    Posts
    14
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Shrink QMainWindow to Minimum Size

    My Google searches were not encouraging but I'll still give it a try here:
    Is there an easy way to shrink a QMainWindow to its minimum size? The problem I have is the following. In my application I have a QDockWidget that is hidden by default. When the user toggles the visibility of the QDockWidget on, the QMainWindow expands to accommodate the dock. However, once the user closes or floats the dock I would like QMainWindow to return to its original size.

  2. #2
    Join Date
    Jan 2006
    Location
    Boston, MA
    Posts
    40
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Shrink QMainWindow to Minimum Size

    Just set right size policy and call adjustSize() for your mainwindow on dock hiding event.

    So, in main window constructor: this->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
    On dock hiding event adjustSize() for your qmainwindow.
    Last edited by fanat9; 17th February 2008 at 03:07.

  3. The following user says thank you to fanat9 for this useful post:

    kloffy (17th February 2008)

  4. #3
    Join Date
    Aug 2007
    Posts
    14
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Shrink QMainWindow to Minimum Size

    That's pretty close, but not quite. Now the window changes its size 3 times. At the beginning it is nice and small, the way I want it to be. Then when I toggle the dock, it roughly doubles its size in addition to adding space to the dock. Then I toggle the dock off, the additional space for the dock dissappears, but the window stays roughly twice as big as it was on startup.

    Edit: Here's the relevant code:
    Qt Code:
    1. ApplicationWindow::ApplicationWindow() : QMainWindow(NULL, Qt::WindowTitleHint | Qt::WindowMinimizeButtonHint | Qt::WindowSystemMenuHint)
    2. {
    3. setWindowTitle(tr("QTApp"));
    4. setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
    5.  
    6. buttonStart = new QPushButton(tr("Start"));
    7. buttonStart->setDefault(true);
    8.  
    9. connect(buttonStart, SIGNAL(clicked()), this, SLOT(start()));
    10.  
    11. center = new QWidget(this);
    12. center->setMinimumSize(400, 150);
    13.  
    14. QGroupBox *groupBoxOptions = new QGroupBox(tr("Options"));
    15. QGridLayout *layoutOptions = new QGridLayout;
    16.  
    17. exepathEdit = new QLineEdit(groupBoxOptions);
    18. cmdlineEdit = new QLineEdit(groupBoxOptions);
    19.  
    20. layoutOptions->addWidget(new QLabel(tr("Exepath:")), 1, 1);
    21. layoutOptions->addWidget(exepathEdit, 1, 2);
    22.  
    23. layoutOptions->addWidget(new QLabel(tr("Cmdline:")), 2, 1);
    24. layoutOptions->addWidget(cmdlineEdit, 2, 2);
    25.  
    26. layoutOptions->setColumnStretch(1, 20);
    27. layoutOptions->setColumnStretch(2, 80);
    28.  
    29. groupBoxOptions->setLayout(layoutOptions);
    30.  
    31. textEdit = new QTextEdit(this);
    32. textEdit->setReadOnly(true);
    33.  
    34. QDockWidget *dock = new QDockWidget(tr("Output"), this);
    35. dock->setAllowedAreas(Qt::BottomDockWidgetArea);
    36. dock->setFeatures(QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetFloatable);
    37. dock->setWidget(textEdit);
    38.  
    39. addDockWidget(Qt::BottomDockWidgetArea, dock);
    40. viewMenu->addAction(dock->toggleViewAction());
    41.  
    42. dock->hide();
    43.  
    44. connect(dock, SIGNAL(visibilityChanged(bool)), this, SLOT(adjust()));
    45.  
    46. QHBoxLayout *buttonLayout = new QHBoxLayout;
    47. buttonLayout->addStretch(1);
    48. buttonLayout->addWidget(buttonStart);
    49. buttonLayout->addStretch(1);
    50.  
    51. QVBoxLayout *centerLayout = new QVBoxLayout;
    52. centerLayout->addWidget(groupBoxOptions);
    53. centerLayout->addLayout(buttonLayout);
    54. center->setLayout(centerLayout);
    55.  
    56. setCentralWidget(center);
    57. }
    58.  
    59. void ApplicationWindow::adjust()
    60. {
    61. adjustSize();
    62. }
    To copy to clipboard, switch view to plain text mode 

    Edit: In addition to that I also tried overriding sizeHint instead of specifing the minimum size on the center widget. That resulted in the old behavior. (Window expands on dock toggle, stays big)
    Last edited by kloffy; 17th February 2008 at 03:41.

  5. #4
    Join Date
    Jan 2006
    Location
    Boston, MA
    Posts
    40
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Shrink QMainWindow to Minimum Size

    Well, again you have to set correct size/size policy for all widgets. Check sizeHint's for your widgets before and after resizing - it will tell you a lot =)
    ...override sizeHint ? Try to set maximum, not minimum size =)

  6. #5
    Join Date
    Aug 2007
    Posts
    14
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Shrink QMainWindow to Minimum Size

    After trying out various combinations I came up with this:

    Qt Code:
    1. ApplicationWindow::ApplicationWindow(QWidget* parent) : QMainWindow(parent, Qt::WindowTitleHint | Qt::WindowMinimizeButtonHint | Qt::WindowSystemMenuHint)
    2. {
    3. setWindowTitle(tr("QTApp"));
    4. setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
    5. setMinimumSize(400, 150);
    6.  
    7. createActions();
    8. createMenus();
    9.  
    10. center = new QWidget(this);
    11. center->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
    12.  
    13. QStringList types;
    14. types << "Executable Files (*.exe)" << "Any files (*)";
    15.  
    16. fileDialog = new QFileDialog(this);
    17. fileDialog->setWindowTitle(tr("Select a program"));
    18. fileDialog->setAcceptMode(QFileDialog::AcceptOpen);
    19. fileDialog->setFilters(types);
    20.  
    21. QGroupBox *groupBoxOptions = new QGroupBox(tr("Options"), center);
    22. QGridLayout *layoutOptions = new QGridLayout(groupBoxOptions);
    23.  
    24. exepathEdit = new QLineEdit(groupBoxOptions);
    25. cmdlineEdit = new QLineEdit(groupBoxOptions);
    26.  
    27. buttonSelectFile = new QPushButton(tr("Select..."), groupBoxOptions);
    28.  
    29. connect(buttonSelectFile, SIGNAL(clicked()), this, SLOT(selectFile()));
    30.  
    31. layoutOptions->addWidget(new QLabel(tr("Exepath:")), 1, 1);
    32. layoutOptions->addWidget(exepathEdit, 1, 2);
    33. layoutOptions->addWidget(buttonSelectFile, 1, 3);
    34.  
    35. layoutOptions->addWidget(new QLabel(tr("Cmdline:")), 2, 1);
    36. layoutOptions->addWidget(cmdlineEdit, 2, 2, 1, 2);
    37.  
    38. layoutOptions->setColumnStretch(1, 20);
    39. layoutOptions->setColumnStretch(2, 60);
    40. layoutOptions->setColumnStretch(3, 20);
    41.  
    42. groupBoxOptions->setLayout(layoutOptions);
    43.  
    44. dock = new QDockWidget(tr("Output"), this);
    45. dock->setAllowedAreas(Qt::BottomDockWidgetArea);
    46. dock->setFeatures(QDockWidget::DockWidgetClosable);
    47.  
    48. textEdit = new QTextEdit(dock);
    49. textEdit->setReadOnly(true);
    50. textEdit->setFixedHeight(100);
    51.  
    52. dock->setWidget(textEdit);
    53.  
    54. addDockWidget(Qt::BottomDockWidgetArea, dock);
    55.  
    56. viewMenu->addAction(dock->toggleViewAction());
    57.  
    58. connect(dock, SIGNAL(visibilityChanged(bool)), this, SLOT(adjust()));
    59.  
    60. buttonStart = new QPushButton(tr("Start"));
    61. buttonStart->setDefault(true);
    62.  
    63. connect(buttonStart, SIGNAL(clicked()), this, SLOT(start()));
    64.  
    65. QHBoxLayout *buttonLayout = new QHBoxLayout;
    66. buttonLayout->addStretch(1);
    67. buttonLayout->addWidget(buttonStart);
    68. buttonLayout->addStretch(1);
    69.  
    70. QVBoxLayout *centerLayout = new QVBoxLayout;
    71. centerLayout->addWidget(groupBoxOptions);
    72. centerLayout->addLayout(buttonLayout);
    73. center->setLayout(centerLayout);
    74.  
    75. setCentralWidget(center);
    76. }
    77.  
    78. void ApplicationWindow::adjust()
    79. {
    80. adjustSize();
    81. }
    To copy to clipboard, switch view to plain text mode 

    There is something about having a minimumSize on the QMainWindow and a fixedHeight on the dock widget that makes it work. It's not a nice solution - I had to sacrifice floating and the dock widget cannot be resized - but it'll do for now. I would still love to find a propper way of doing this. Here's a quick and dirty ASCII art:

    Qt Code:
    1. Without Dock:
    2. ____________________
    3. |Main |
    4. |--------------------|
    5. | .-Options---------.|
    6. | | Exe: [Blablub ] ||
    7. | | Cmd: [Blablub ] ||
    8. | |_________________||
    9. | [Start] |
    10. |____________________|
    11.  
    12. With dock:
    13. ____________________
    14. |Main |
    15. |--------------------|
    16. | .-Options---------.|
    17. | | Exe: [Blablub ] ||
    18. | | Cmd: [Blablub ] ||
    19. | |_________________||
    20. | [Start] |
    21. |--------------------|
    22. |Output x|
    23. | .-----------------.|
    24. | |Blablub... ||
    25. | |Blablub... ||
    26. | | ||
    27. | |_________________||
    28. |____________________|
    To copy to clipboard, switch view to plain text mode 
    It doesn't have to be a dock, if anyone knows a custom widget that allows you to do something like this, I would appreciate if you point me to it.

Similar Threads

  1. Qt Layout engine not respecting minimum size?
    By Mike in forum Qt Programming
    Replies: 2
    Last Post: 28th August 2007, 14:27
  2. QMenuBar minimum size
    By Angelo Moriconi in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2007, 22:14
  3. Qt 4.1.1 linker warnings
    By Matt Smith in forum Installation and Deployment
    Replies: 0
    Last Post: 26th February 2006, 23:14

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.