Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. QVBoxLayout - Centering the Top Widget

QVBoxLayout - Centering the Top Widget

Scheduled Pinned Locked Moved Unsolved Brainstorm
2 Posts 2 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Drakkhan
    wrote on last edited by
    #1

    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();
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved