【发布时间】:2019-01-24 17:08:38
【问题描述】:
我的环境:
- Windows 10,64 位
- Microsoft Visual Studio 2015
- Qt 5.6.2
- Qt Creator 4.8.1
我有一个包含滚动区域的对话框。滚动区域最初是空的:
当用户点击复选框时,小部件被添加到滚动区域:
从上面可以看出,只显示了滚动区域的一部分。我想自动放大它(以及包含它的对话框),所以结果看起来像这样:
我的代码中缺少什么?
我已将问题简化为一个最小的示例程序(上面的图片是从中拍摄的)。代码如下:
scroll_area.pro:
QT += widgets
TEMPLATE = app
SOURCES += main.cpp my_dialog.cpp
HEADERS += my_dialog.h my_widget.h
TARGET = ScrollArea
main.cpp:
#include "my_dialog.h"
#include <QApplication>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
MyDialog myDialog;
myDialog.show();
return app.exec();
}
my_dialog.h:
#ifndef MY_DIALOG_H
#define MY_DIALOG_H
#include <QBoxLayout>
#include <QDialog>
#include <QScrollArea>
class MyDialog : public QDialog
{
Q_OBJECT
public:
MyDialog();
private:
QScrollArea* m_scrollArea;
QHBoxLayout* m_hLayout;
private slots:
void changeScrollArea(int newState);
};
#endif // MY_DIALOG_H
my_dialog.cpp:
#include "my_dialog.h"
#include "my_widget.h"
#include <QCheckBox>
#include <QDialogButtonBox>
MyDialog::MyDialog() :
QDialog(nullptr), m_scrollArea(new QScrollArea(this)), m_hLayout(new QHBoxLayout())
{
setWindowTitle("My Dialog");
QVBoxLayout* vlayout = new QVBoxLayout(this);
// Set up the scroll area.
m_scrollArea->setBackgroundRole(QPalette::ColorRole::Light);
m_scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
vlayout->addWidget(m_scrollArea);
// Set up the scroll contents.
QWidget* scrollContents = new QWidget(this);
scrollContents->setLayout(m_hLayout);
m_scrollArea->setWidget(scrollContents);
// Set up the check box.
QCheckBox* checkbox = new QCheckBox("Check to add widgets", this);
connect(checkbox, SIGNAL(stateChanged(int)), this, SLOT(changeScrollArea(int)));
vlayout->addWidget(checkbox);
}
void MyDialog::changeScrollArea(int newState)
{
// Set up the horizontal layout.
m_hLayout->deleteLater();
m_hLayout = new QHBoxLayout();
// Set up the scroll contents.
QWidget* scrollContents = new QWidget(this);
scrollContents->setLayout(m_hLayout);
if (newState == Qt::Checked)
{
m_hLayout->addWidget(new MyWidget(scrollContents));
m_hLayout->addWidget(new MyWidget(scrollContents));
}
m_scrollArea->setWidget(scrollContents);
}
my_widget.h
#ifndef MY_WIDGET_H
#define MY_WIDGET_H
#include <QGridLayout>
#include <QPushButton>
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget* parent) : QWidget(parent)
{
QGridLayout* gridLayout = new QGridLayout(this);
for (int row = 0; row < 5; ++row)
{
for (int column = 0; column < 1; ++column)
{
QPushButton* button = new QPushButton("FOO");
gridLayout->addWidget(button, row, column);
}
}
}
};
#endif // MY_WIDGET_H
【问题讨论】:
标签: c++ qt qscrollarea