gpt4 book ai didi

c++ - 如果 child 调整大小,让 QWidget 和 QVBoxLayout 自动调整大小(Qt4)

转载 作者:行者123 更新时间:2023-11-30 04:16:08 24 4
gpt4 key购买 nike

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

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 正确增长?

最佳答案

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

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

关于c++ - 如果 child 调整大小,让 QWidget 和 QVBoxLayout 自动调整大小(Qt4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17900046/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com