Results 1 to 5 of 5

Thread: Shrink QMainWindow to Minimum Size

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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 =)

  2. #2
    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, 13:27
  2. QMenuBar minimum size
    By Angelo Moriconi in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2007, 21:14
  3. Qt 4.1.1 linker warnings
    By Matt Smith in forum Installation and Deployment
    Replies: 0
    Last Post: 26th February 2006, 22: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
  •  
Qt is a trademark of The Qt Company.