[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
    {
    Q_OBJECT
    public:
    MainWindow(QWidget parent = 0, Qt::WFlags flags = 0): QMainWindow(parent, flags)
    {
    ui.setupUi(this);
    // Create group box
    _groupBox = new QGroupBox();
    _groupBox->sizePolicy().setHeightForWidth(true);
    _groupBox->setMinimumWidth(200);
    _groupBox->setMinimumHeight(200);
    if(_groupBox->sizePolicy().hasHeightForWidth())
    std::cout << "It never gets in here! :o" << std::endl;
    // Create v layout and attach group box
    _vLayout = new CustomVLayout();
    _vLayout->addWidget(_groupBox);
    // Create layout widget and attack the v layout
    _layoutWidget = new QWidget();
    _layoutWidget->setLayout(_vLayout);
    // Attach to the dock the layout widget as a child widget.
    _dockWidget = new QDockWidget();
    _dockWidget->setWidget(_layoutWidget);
    // Add dock widget to the main window
    addDockWidget(Qt::LeftDockWidgetArea,_dockWidget);
    }
    ~MainWindow(){}
    private:
    Ui::MainWindowClass ui;
    QDockWidget
    _dockWidget;
    QGroupBox* _groupBox;
    CustomVLayout* _vLayout;
    QWidget* _layoutWidget;
    };

    // CustomVLayout.h
    class CustomVLayout : public QVBoxLayout
    {
    protected:
    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.: pastebin.com)

    Thanks a lot for your time!

    Cheers!



  • Hi,

    Take a look at "these hints":http://stackoverflow.com/questions/452333/how-to-maintain-widgets-aspect-ratio-in-qt and check out the provided links at Further reading.

    Regards,
    Leon



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

    Cheers!

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

    Take a look at "these hints":http://stackoverflow.com/questions/452333/how-to-maintain-widgets-aspect-ratio-in-qt and check out the provided links at Further reading.

    Regards,
    Leon[/quote]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.