Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: "Expanding" doesn't work inside a layout

Tags:

qt

My purpose is to create a scrollable control with a QVBoxLayout inside of it that has various controls (say buttons) on it. That control is put on a *.ui form. In the constructor for that control I write the following code:

    MyScrollArea::MyScrollArea(QWidget *parent) :
        QScrollArea(parent)
    {
        // create the scrollable container

        this->container = new QWidget();  // container widget member
        this->container->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        this->container->setContentsMargins(QMargins(0,0,0,0));

        this->content = new QVBoxLayout(); // layout member
        this->content->setMargin(0);
        this->content->setSpacing(0);

        for (int i=0; i<100; i++)
        {
            QPushButton * widget = new QPushButton();
            widget->setText("button");
            widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
            this->content->addWidget(widget);
        }

        this->container->setLayout(this->content);
        this->content->layout();

        this->setWidget(this->container);
   }

My problem: the buttons have a fixed size and do not expand horizontally. they have a fixed size. i'd like them to expand horizontally to fill the row they're in. How can I get them expanding horizontally across their parent container?

like image 674
JasonGenX Avatar asked Dec 07 '25 13:12

JasonGenX


1 Answers

Try calling this->setWidgetResizable(true);

like image 134
Arnold Spence Avatar answered Dec 11 '25 06:12

Arnold Spence



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!