【问题标题】:Letting QWidget and QVBoxLayout automatically resize if child resizes (Qt4)如果孩子调整大小,让 QWidget 和 QVBoxLayout 自动调整大小(Qt4)
【发布时间】:2013-07-27 16:31:30
【问题描述】:

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

ma​​in.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() 解决方案,但它不起作用:(link)。我注释掉的所有东西都是我尝试过但没有帮助的东西。

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

【问题讨论】:

    标签: c++ qt user-interface qt4


    【解决方案1】:

    这是一个适合我的简化版本:

    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;
    };
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-02-15
        • 1970-01-01
        • 1970-01-01
        • 2011-11-23
        • 1970-01-01
        • 1970-01-01
        • 2018-03-24
        相关资源
        最近更新 更多