I have some problem placing custom widget in QScrollArea. My custom widget contains 4 labels in QGridLayout filling it. Now I want to keep this widget in QScrollArea and be able to add more labels to it but I want only 4 of them to be shown in viewport.
So that's how widget with 4 labels looks like in QScrollArea:
And here's the widget in QScrollArea after adding two more labels, where the red rectangle is the viewport.

How can I achieve such result?
===================================
UPDATE
I eventually resolved my issue with the following code. It may need some minor spacing fixes.
#include "QtGuiApplication2.h"
#include "qscrollarea.h"
#include "CustomWidget.h"
QtGuiApplication2::QtGuiApplication2(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
QScrollArea * qScrollArea = new QScrollArea();
CustomWidget * customWidget = new CustomWidget(this);
qScrollArea->setWidget(customWidget);
qScrollArea->setWidgetResizable(true);
qScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
ui.mainLayout->addWidget(qScrollArea, 1, 1, 1, 1);
}
CustomWidget class:
#include "CustomWidget.h"
#include "qlabel.h"
CustomWidget::CustomWidget(QWidget *parent) : QWidget(parent)
{
labelsNum = 4;
rows = 2;
layout = new QGridLayout();
this->setLayout(layout);
QMargins * margin = new QMargins(10, 10, 10, 10);
layout->setContentsMargins(*margin);
layout->setHorizontalSpacing(5);
layout->setVerticalSpacing(5);
initLabels();
addLabels();
}
CustomWidget::~CustomWidget()
{
}
void CustomWidget::initLabels()
{
int cols = labelsNum / rows;
for (int i = 0; i < labelsNum; i++)
{
CustomLabel * label = new CustomLabel(this);
label->setText(QString::number(i));
label->setFrameShape(QFrame::Box);
labels.append(label);
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
layout->addWidget(labels.at(i * cols + j), i + 1, j + 1, 1, 1);
}
}
}
void CustomWidget::addLabels()
{
int numLabels = labels.size();
for (int i = 0; i < 2; i++)
{
CustomLabel * label = new CustomLabel(this);
label->setText(QString::number(numLabels + i));
label->setFrameShape(QFrame::Box);
labels.append(label);
}
labelsNum += rows;
int cols = labelsNum / rows;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
layout->addWidget(labels.at(i * cols + j), i + 1, j + 1, 1, 1);
}
}
}
void CustomWidget::resizeEvent(QResizeEvent * e)
{
QWidget::resizeEvent(e);
QSize size = viewportSize;
//Substract all the spacing from view size
int horizontalSpacing = ((4 / rows) - 1) * layout->horizontalSpacing();
int verticalSpacing = (rows - 1) * layout->verticalSpacing();
size -= QSize(layout->margin() * 2 + horizontalSpacing, layout->margin() * 2 + verticalSpacing);
size *= 0.5;
for (int i = 0; i < labels.size(); i++)
{
labels.at(i)->resizeEvent(e, size);
}
}
And custom label:
#include "CustomLabel.h"
CustomLabel::CustomLabel(QWidget *parent): QLabel(parent)
{
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
setMinimumSize(QSize(50, 50));
}
CustomLabel::~CustomLabel()
{
}
void CustomLabel::resizeEvent(QResizeEvent * e, QSize size)
{
this->setFixedSize(size);
}
