如果子项调整大小,则让QWidget和QVBoxLayout自动调整大小(Qt4)

Letting QWidget and QVBoxLayout automatically resize if child resizes (Qt4)

本文关键字:调整 QVBoxLayout Qt4 则让 如果 QWidget      更新时间:2023-10-16

我给出了以下最小示例代码。

main.cpp:

#include <QApplication>
#include "qt.h"
int main(int argc, char** argv)
{
    QApplication app(argc, argv);
    MyDialog mainWin;
    mainWin.show();
    return app.exec();
}

qt.cpp:

#include <QLabel>
#include "qt.h"
void MyDialog::setupUi()
{
    setCentralWidget(new QWidget);
    mainLayout = new QVBoxLayout( centralWidget() );
    centralWidget()->setLayout(mainLayout);
    // show the add new effect channel button
    QPushButton* newKnobBtn = new QPushButton("new", this );
    connect( newKnobBtn, SIGNAL(clicked()), this, SLOT(addNewKnob()));
    mainLayout->addWidget( newKnobBtn, 0, Qt::AlignRight );
    containerWidget = new QWidget(this);
    scrollArea = new QScrollArea(containerWidget);
    mainLayout->addWidget(containerWidget);
    scrollLayout = new QVBoxLayout(scrollArea);
    scrollArea->setLayout(scrollLayout);
    /*
    QSizePolicy pol;
    pol.setVerticalPolicy(QSizePolicy::Expanding);
    setSizePolicy(pol);
    */
    addNewKnob(); // to fit size initially
}
void MyDialog::addNewKnob()
{
    scrollLayout->addWidget(new QLabel("Hello World", this));
    /*
    containerWidget->adjustSize();
    adjustSize();
    */
}

qt.h:

#include <QMainWindow>
#include <QVBoxLayout>
#include <QScrollArea>
#include <QPushButton>
class MyDialog : public QMainWindow
{
    Q_OBJECT
private slots:
    void addNewKnob();
private:
    void setupUi();
    QVBoxLayout* mainLayout;
    QScrollArea* scrollArea;
    QVBoxLayout* scrollLayout;
    QWidget* containerWidget;
public:
    MyDialog( ) { setupUi(); }
};

编译:将所有内容放在一个目录中,键入

qmake -project && qmake && make

我从这里得到了adjustSize()解决方案,但它不起作用:(链接)。我所评论的都是我尝试过但没有帮助的事情。

当向scrollLayout添加新标签时,如何使containerWidgetscrollLayout正确增长?

这里有一个适用于我的简化版本:

qt.cpp:

#include <QLabel>
#include <QPushButton>
#include <QScrollArea>
#include "qt.h"
MyDialog::MyDialog()
{
    QWidget * mainWidget = new QWidget;
    QBoxLayout * mainLayout = new QVBoxLayout(mainWidget);
    setCentralWidget(mainWidget);
    // show the add new effect channel button
    QPushButton* newKnobBtn = new QPushButton("new");
    connect( newKnobBtn, SIGNAL(clicked()), this, SLOT(addNewKnob()));
    mainLayout->addWidget( newKnobBtn, 0, Qt::AlignRight );
    QScrollArea * scrollArea = new QScrollArea;
    scrollArea->setWidgetResizable(true);
    mainLayout->addWidget(scrollArea);
    QWidget * labelsWidget = new QWidget;
    labelsLayout = new QVBoxLayout(labelsWidget);
    scrollArea->setWidget(labelsWidget);
    addNewKnob(); // to fit size initially
}
void MyDialog::addNewKnob()
{
    labelsLayout->addWidget(new QLabel("Hello World"));
}

qt.h:

#include <QMainWindow>
#include <QBoxLayout>
class MyDialog : public QMainWindow
{
    Q_OBJECT
public:
    MyDialog( );
private slots:
    void addNewKnob();
private:
    QBoxLayout * labelsLayout;
};

您的containerWidget只包含一个QScrollArea。我不知道你为什么需要这个。但是,如果您出于某种原因需要这样做,则需要向该小部件添加布局,以便使布局正常工作。也不要为QScrollArea创建布局。它已经有了内部实现的布局。您应该将scrollLayout添加到滚动区域的viewport()小部件中。

构造布局并将小部件传递给其构造函数时,布局会自动分配给传递的小部件。在此之后,您不应再拨打setLayout。此操作将无效,并产生控制台警告。