Important: Please read the Qt Code of Conduct -

[Solved] How to Preserve the aspect ratio of a widget?

  • Hello everyone!

    I am trying to do something with Qt but no luck so far.

    • I have an application with QMainWindow (the one you get when you create a new Qt Application from Visual Studio 2008).
    • I attach a QDockWidget at the left side of the QMainWindow.
    • I create a QWidget and I add it as a child to the QDockWidget. Then I use setLayout() of the previously created widget to set a "CustomLayout" ( "CustomLayout" is a custom layout which inherits from QVBoxLayout) on it. I read online that if you want to set a layout to a QDockWidget this is the right way to do it. Is this right? (the QDockWidget::setLayout() was giving me an error).
    • I create a QGroupBox and I attach it to the previously created CustomLayout.

    So far so good. Everything works as expected. When I resize the QDockWidget (by clicking its right edge and dragging to the right) the QGroupBox stretches with it.
    What I want is to keep the same aspect ratio on the QGroupBox. i.e.: Change the height of the QGroupBox programmaticaly to keep the same aspect ratio. I read that the right way of doing this is to use my own custom layout for the layout were QGroupBox is going to be attached, re-implement the function "int heightForWidth(int) const" and use QGroupBox::sizePolicy().setHeightForWidth(true).
    I do all the above but nothing happens. The CustomLayout::heightForWidth() function is never called. If after all the above, if I do a QGroupBox::sizePolicy().hasHeightForWidth() it returns false. Am I doing something wrong?
    What is the right way to keep the aspect ratio of the QGroupBox?
    This is my code:

    // mainwindow.h
    class MainWindow : public QMainWindow
    MainWindow(QWidget parent = 0, Qt::WFlags flags = 0): QMainWindow(parent, flags)
    // Create group box
    _groupBox = new QGroupBox();
    std::cout << "It never gets in here! :o" << std::endl;
    // Create v layout and attach group box
    _vLayout = new CustomVLayout();
    // Create layout widget and attack the v layout
    _layoutWidget = new QWidget();
    // Attach to the dock the layout widget as a child widget.
    _dockWidget = new QDockWidget();
    // Add dock widget to the main window
    Ui::MainWindowClass ui;
    QGroupBox* _groupBox;
    CustomVLayout* _vLayout;
    QWidget* _layoutWidget;

    // CustomVLayout.h
    class CustomVLayout : public QVBoxLayout
    int heightForWidth(int width) const
    std::cout << "heightForWidth(" << width << ")" << std::endl;
    return 20; // Just return a constant number for now. Only for testing.

    I am really sorry for the long code. I wasn't sure if it is allowed to give you a link of some "paste-site" (e.g.:

    Thanks a lot for your time!


  • Hi,

    Take a look at "these hints": and check out the provided links at Further reading.


  • Thank you very much dude. I followed the tutorials from the links in that page and I did it! :D


    [quote author="leon.anavi" date="1306322372"]Hi,

    Take a look at "these hints": and check out the provided links at Further reading.


Log in to reply