QGroupBox, QScrollArea, QFormLayout
-
Assuming margins at set to zero:
pFormLayout=new QFormLayout; pFormLayout->setContentsMargins(0,0,0,0); pFormLayout->setSpacing(0); pFormLayout->setHorizontalSpacing(10);
In the show event you can calculate 3 rows height like that:
void Widget::showEvent(QShowEvent* event) { QWidget::showEvent(event); QLayoutItem * item=pFormLayout-> itemAt(0); int h=item->geometry().height(); pScrollArea->setFixedHeight(h*3+2); }
-
@SPlatten
I assumed that the formLayout is not empty, unless the app will crash.If you create one row in the constructor and want an empty form on show up, you can do:
QWidget::showEvent(event);
QLayoutItem* item=pFormLayout-> itemAt(0);
int h=item->geometry().height();
pScrollArea->setFixedHeight(h3+3);
pFormLayout->removeRow(0);[WARNING]
Multi showEvent can occur, you need to add some flag to prevent that:void Widget::showEvent(QShowEvent* event) { static bool firstShow=true; QWidget::showEvent(event); if(firstShow) { QLayoutItem * item=pFormLayout-> itemAt(0); int h=item->geometry().height(); pScrollArea->setFixedHeight(h*3+3); pFormLayout->removeRow(0); firstShow=false; } }
-
@SPlatten
I assumed that the formLayout is not empty, unless the app will crash.If you create one row in the constructor and want an empty form on show up, you can do:
QWidget::showEvent(event);
QLayoutItem* item=pFormLayout-> itemAt(0);
int h=item->geometry().height();
pScrollArea->setFixedHeight(h3+3);
pFormLayout->removeRow(0);[WARNING]
Multi showEvent can occur, you need to add some flag to prevent that:void Widget::showEvent(QShowEvent* event) { static bool firstShow=true; QWidget::showEvent(event); if(firstShow) { QLayoutItem * item=pFormLayout-> itemAt(0); int h=item->geometry().height(); pScrollArea->setFixedHeight(h*3+3); pFormLayout->removeRow(0); firstShow=false; } }
-
@SPlatten Q
When you use font size to set item height, it is better to use some reference chars. That is what I do:
Rect bounding_rect = QFontMetrics( font ).tightBoundingRect( QString( "MWLPGHXYZmwlpghxyz" ) );
use bounding_rect.height();
And item height has to be a bit bigger than this height. -
@SPlatten
I assumed that the formLayout is not empty, unless the app will crash.If you create one row in the constructor and want an empty form on show up, you can do:
QWidget::showEvent(event);
QLayoutItem* item=pFormLayout-> itemAt(0);
int h=item->geometry().height();
pScrollArea->setFixedHeight(h3+3);
pFormLayout->removeRow(0);[WARNING]
Multi showEvent can occur, you need to add some flag to prevent that:void Widget::showEvent(QShowEvent* event) { static bool firstShow=true; QWidget::showEvent(event); if(firstShow) { QLayoutItem * item=pFormLayout-> itemAt(0); int h=item->geometry().height(); pScrollArea->setFixedHeight(h*3+3); pFormLayout->removeRow(0); firstShow=false; } }
-
@mpergand , how is the showEvent connected and to which widget? I assume this is the MainWindow instance that this code is relevant to?
-
@SPlatten
Here Widget is the top level object, but it does not have to be.how is the showEvent connected
It's connected to nothing, it's a virtual method of QWidget.
-
Widget.h
#include <QWidget> class QScrollArea; class QFormLayout; class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = nullptr); private: void showEvent(QShowEvent* event); QScrollArea* pScrollArea; QFormLayout* pFormLayout; };
Widget.cpp
Widget::Widget(QWidget *parent) : QWidget(parent) { auto vLayout=new QVBoxLayout; setLayout(vLayout); pScrollArea=new QScrollArea; vLayout->addWidget(pScrollArea); auto sWidget=new QWidget; pScrollArea->setWidget(sWidget); pScrollArea->setWidgetResizable(true); pFormLayout=new QFormLayout; pFormLayout->setContentsMargins(0,0,0,1); pFormLayout->setSpacing(0); pFormLayout->setHorizontalSpacing(10); sWidget->setLayout(pFormLayout); // add one row for calculation in showEvent pFormLayout->addRow("", new QLabel()); vLayout->addWidget(new QPushButton("Clear")); }
-
Widget.h
#include <QWidget> class QScrollArea; class QFormLayout; class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = nullptr); private: void showEvent(QShowEvent* event); QScrollArea* pScrollArea; QFormLayout* pFormLayout; };
Widget.cpp
Widget::Widget(QWidget *parent) : QWidget(parent) { auto vLayout=new QVBoxLayout; setLayout(vLayout); pScrollArea=new QScrollArea; vLayout->addWidget(pScrollArea); auto sWidget=new QWidget; pScrollArea->setWidget(sWidget); pScrollArea->setWidgetResizable(true); pFormLayout=new QFormLayout; pFormLayout->setContentsMargins(0,0,0,1); pFormLayout->setSpacing(0); pFormLayout->setHorizontalSpacing(10); sWidget->setLayout(pFormLayout); // add one row for calculation in showEvent pFormLayout->addRow("", new QLabel()); vLayout->addWidget(new QPushButton("Clear")); }
-
Widget.h
#include <QWidget> class QScrollArea; class QFormLayout; class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = nullptr); private: void showEvent(QShowEvent* event); QScrollArea* pScrollArea; QFormLayout* pFormLayout; };
Widget.cpp
Widget::Widget(QWidget *parent) : QWidget(parent) { auto vLayout=new QVBoxLayout; setLayout(vLayout); pScrollArea=new QScrollArea; vLayout->addWidget(pScrollArea); auto sWidget=new QWidget; pScrollArea->setWidget(sWidget); pScrollArea->setWidgetResizable(true); pFormLayout=new QFormLayout; pFormLayout->setContentsMargins(0,0,0,1); pFormLayout->setSpacing(0); pFormLayout->setHorizontalSpacing(10); sWidget->setLayout(pFormLayout); // add one row for calculation in showEvent pFormLayout->addRow("", new QLabel()); vLayout->addWidget(new QPushButton("Clear")); }
@mpergand , Thank you, works a treat...now to port to my main application:
ui->setupUi(this); QVBoxLayout* pvbxLayout(new QVBoxLayout); mpsa = new QScrollArea; pvbxLayout->addWidget(mpsa); QWidget* pContainer(new QWidget); mpsa->setWidget(pContainer); int intFixedHeight(fontMetrics().height() * 3); mpsa->setFixedHeight(intFixedHeight); mpsa->setWidgetResizable(true); mpFormLayout = new QFormLayout; mpFormLayout->setContentsMargins(0,0,0,0); mpFormLayout->setSpacing(0); mpFormLayout->setHorizontalSpacing(10); pContainer->setLayout(mpFormLayout); for( int i=1; i<=10; i++ ) { mpFormLayout->addRow(QString::number(i), new QLabel(QString("Hello World: %1").arg(i))); } pvbxLayout->addWidget(new QPushButton("HELLO")); ui->centralwidget->setLayout(pvbxLayout);