Below is a massively stripped down code of an application I have. The application uses different QGroupBoxes for data display. The lowest QGroupBox has a QScrollArea which contains a changing (and sometimes large) number of labels. Thus I need a scrolling area. Code works, but wastes a lot of space above and below the labels. In my case 113 pixel of which about 30 below and 30 above are wasted. I’d like to get rid of them but can’t find a way. Using the size policy and the zero margins already helped a bit, but it’s still not enough. I experimented with many more size policies and margins at different components, but nothing helps.
I’d appreciate anything, which could reduce the lowest “Data rates” box to the vertical minimum.
makefile:
CPP = g++
MOC = /usr/libexec/qt6/moc
QTOPTS = $(shell pkg-config Qt6Core Qt6Widgets Qt6Gui --cflags --libs)
testerr: qmainwindow.moc
$(CPP) qmainwindow.cpp $(QTOPTS) -o $@
%.moc: %.h
$(MOC) $? -o $@
clean:
-rm *.moc testerr
qmainwindow.h
#include <QtCore/QObject>
#include <QtCore/QTimer>
#include <QtWidgets/QApplication>
#include <QtWidgets/QBoxLayout>
#include <QtWidgets/QDialog>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QGroupBox>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLayoutItem>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QScrollArea>
#include <QtWidgets/QTextEdit>
#include <QtWidgets/QWidget>
class qMainWindow : public QWidget
{
Q_OBJECT
public:
qMainWindow(QWidget *par = 0);
private slots:
void actionPressed(void);
void actionPressedHide(void) {DataRates->hide();};
private:
int Started;
QPushButton * Button;
QPushButton * Hide;
QTextEdit * List;
QGroupBox * DataRates;
QWidget* scrollAreaContent;
QScrollArea* scrollArea;
QList<QLabel *> Labels;
};
qmainwindow.cpp
#include "qmainwindow.moc"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
qMainWindow main;
main.show();
main.resize(700,550);
return app.exec();
}
qMainWindow::qMainWindow(QWidget *par) : QWidget(par)
{
QVBoxLayout *outerlayout = new QVBoxLayout;
QGroupBox *box = new QGroupBox(tr("Data"));
outerlayout->addWidget(box);
List = new QTextEdit();
QGridLayout *glayout = new QGridLayout();
Button = new QPushButton(tr("Do"));
connect(Button, SIGNAL(clicked(void)), this, SLOT(actionPressed(void)));
glayout->addWidget(Button, 0, 0);
Hide = new QPushButton(tr("Hide"));
connect(Hide, SIGNAL(clicked(void)), this, SLOT(actionPressedHide(void)));
glayout->addWidget(Hide, 0, 1);
glayout->addWidget(List,1,0,1,2);
box->setLayout(glayout);
Started = 0;
DataRates = new QGroupBox(tr("Data rates"));
DataRates->hide();
outerlayout->addWidget(DataRates);
setLayout(outerlayout);
List->document()->setPlainText(QString("Start SizeHint %1 Size %2n")
.arg(DataRates->sizeHint().height()).arg(DataRates->height()));
}
void qMainWindow::actionPressed(void)
{
if(!Started)
{
QLayout *layoutOut;
QLayout *layout;
if(!DataRates->isHidden())
{
DataRates->hide();
layout = DataRates->layout();
if(layout)
{
QLayoutItem *child;
while ((child = layout->takeAt(0)) != 0) {
QWidget *w = child->widget();
if(w)
w->hide();
delete child;
}
delete layout;
}
}
else if((layout = DataRates->layout()))
delete layout;
layoutOut = new QBoxLayout(QBoxLayout::LeftToRight, DataRates);
scrollAreaContent = new QWidget();
layout = new QBoxLayout(QBoxLayout::LeftToRight, scrollAreaContent);
layoutOut->setContentsMargins(0,0,0,0);
scrollAreaContent->setLayout(layout);
scrollArea = new QScrollArea;
scrollArea->setWidgetResizable(true);
scrollArea->setWidget(scrollAreaContent);
scrollArea->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
layoutOut->addWidget(scrollArea);
DataRates->setLayout(layoutOut);
DataRates->hide();
Started = 1;
}
QLabel *label = new QLabel("XXXXXXXX", DataRates);
scrollAreaContent->layout()->addWidget(label);
Labels.append(label);
if(DataRates->isHidden())
{
DataRates->show();
for(int p = 0; p < 1; ++p)
Labels[p]->show();
}
QTextCursor tcursor(List->document());
tcursor.movePosition(QTextCursor::End);
tcursor.insertText(QString("Line SizeHint %1 Size %2n")
.arg(DataRates->sizeHint().height()).arg(DataRates->height()));
}