如果子项调整大小,则让QWidget和QVBoxLayout自动调整大小(Qt4)
Letting QWidget and QVBoxLayout automatically resize if child resizes (Qt4)
我给出了以下最小示例代码。
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
添加新标签时,如何使containerWidget
和scrollLayout
正确增长?
这里有一个适用于我的简化版本:
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
。此操作将无效,并产生控制台警告。
相关文章:
- 为char数组调整zlib-zpipe
- 调整大小后指向元素值的指针unordered_map有效?
- 在C++中调整向量中的索引
- 哪些库可以通过Opencv调整曝光率
- 如何调整 std::vector of Eigen::MatrixXd 的大小
- Alt+Enter 在 Win32 应用中,管理大小调整和分辨率
- 在容量内调整矢量大小时的性能影响
- 将 boost::odeint 与向量类一起使用,而无需调整向量的大小
- 将摄像机调整到俯视图
- Qt 图表条形图调整大小崩溃
- 除非重新绘制大小,否则SDL_Texture在调整大小后呈现黑色
- 调整布局上的 QGraphicsView 小部件的大小
- 如何在构建链接列表时调整头、尾指针
- 如何将旧的 C 样式 #define 映射与现代C++进行调整?
- 当您在此单词中搜索单词时调整字符数组的大小?
- SDL2 调整窗口大小后如何缩放鼠标坐标?
- 如果子项调整大小,则让QWidget和QVBoxLayout自动调整大小(Qt4)
- 我如何调整大小QVBoxLayout填充表单没有空间
- 如何在Qt Creator中添加可调整大小的QVBoxLayout ?
- 如何动态调整QLabel/QVBoxLayout/QWidget中使用的QImage的大小