QVBoxLayout - Centering the Top Widget



  • Hey, I've got a layout problem. Hoping someone can help.

    Background:
    I'm building a window that has two widgets in a QVBoxLayout. The top one (thanks to @J.Hilk : here) maintains a square aspect ratio. The bottom has a fixed height and a minimum width.

    Problem:
    The problem is that when I resize the overall window, the upper widget (forced to be square) sometimes can't stretch across the entire height or width available, and chooses to align left and top. I want it to align center or center depending on which dimension it can't fill.

    Some code illustrating my problem:

    upperwidget.h:

    #ifndef UPPERWIDGET_H
    #define UPPERWIDGET_H
    
    #include <QLabel>
    #include <QResizeEvent>
    
    class UpperWidget : public QLabel
    {
        Q_OBJECT
    public:
        explicit UpperWidget(QWidget *parent = 0);
    
    protected:
        virtual void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE;
    
    signals:
    
    public slots:
    
    };
    
    #endif // UPPERWIDGET_H
    

    upperwidget.cpp:

    #include "upperwidget.h"
    
    UpperWidget::UpperWidget(QWidget *parent) : QLabel(parent)
    {
    
        setText("UpperWidget");
        setFrameStyle(QFrame::Panel | QFrame::Raised);
        setLineWidth(2);
        setAlignment(Qt::AlignCenter | Qt::AlignCenter);
        setMinimumSize(100, 100);
    
    
    }
    
    void UpperWidget::resizeEvent(QResizeEvent *event)
    {
        event->accept();
    
        if(event->size().width() > event->size().height()){
            QWidget::resize(event->size().height(),event->size().height());
        }else{
            QWidget::resize(event->size().width(),event->size().width());
        }
    }
    

    lowerwidget.h:

    #ifndef LOWERWIDGET_H
    #define LOWERWIDGET_H
    
    #include <QLabel>
    
    class LowerWidget : public QLabel
    {
        Q_OBJECT
    public:
        explicit LowerWidget(QWidget *parent = 0);
    
    signals:
    
    public slots:
    
    };
    
    #endif // LOWERWIDGET_H
    

    lowerwidget.cpp:

    #include "lowerwidget.h"
    
    LowerWidget::LowerWidget(QWidget *parent) : QLabel(parent)
    {
        setText("LowerWidget");
        setFrameStyle(QFrame::Panel | QFrame::Raised);
        setLineWidth(2);
        setAlignment(Qt::AlignCenter | Qt::AlignCenter);
        setMinimumSize(100, 50);
        setMaximumHeight(50);
    
    }
    

    main.cpp:

    #include <QApplication>
    #include <QWidget>
    #include <QLayout>
    
    #include "upperwidget.h"
    #include "lowerwidget.h"
    
    int main(int argc, char **argv)
    {
     QApplication app (argc, argv);
    
     QWidget *window = new QWidget;
     UpperWidget *upperWidget = new UpperWidget(window);
     LowerWidget *lowerWidget = new LowerWidget(window);
     QVBoxLayout *theLayout = new QVBoxLayout;
    
     theLayout->addWidget(upperWidget, 0, 0);
     theLayout->addWidget(lowerWidget, 0, 0);
     window->setLayout(theLayout);
     window->show();
    
     return app.exec();
    }
    

  • Lifetime Qt Champion

    Hi,

    From the top of my head, you can try to set alignment flag to Qt::AlignHCenter rather than 0.


Log in to reply