PDA

View Full Version : How to display many widgets in grid?



weixj2003ld
13th June 2020, 05:51
I want to display many widgets in colums and rows(shows as picture),I will not use tablewidget?how to relized it?Thank you.13470

ChrisW67
13th June 2020, 07:39
QGridLayout ?

weixj2003ld
13th June 2020, 09:31
Can QGridLayout draw the grid lines(show as picture ,blue lines) between the Label and Textline?

d_stranz
13th June 2020, 16:03
Can QGridLayout draw the grid lines

No. QGridLayout is a layout, not a widget. It has no visual appearance.

I think you will probably have to write your own class, based on QWidget, to do this. You could also use the Qt Graphics View Framework (https://doc.qt.io/qt-5/graphicsview.html), but either way, the amount of work is about the same.

ChrisW67
14th June 2020, 06:22
This may scratch your itch depending on how fussy you are about the lines.


#include "widget.h"

#include <QGridLayout>
#include <QVBoxLayout>
#include <QFrame>
#include <QLabel>
#include <QLineEdit>

Widget::Widget(QWidget *parent)
: QWidget(parent)
{
setStyleSheet(
QStringLiteral(
"QFrame { border: 1px solid blue; } "
"QLabel, QLineEdit { padding: 5px; border: 1px solid blue; } "
)
);
QVBoxLayout *layout = new QVBoxLayout(this);
QFrame *frame = new QFrame(this);
layout->addWidget(frame);

QGridLayout *gridLayout = new QGridLayout(frame);
gridLayout->setSpacing(0);
gridLayout->setMargin(0);

for (int row = 0; row < 4; ++row) {
QLabel *label = new QLabel(QString("Row %1").arg(row), this);
QLineEdit *edit = new QLineEdit(this);
gridLayout->addWidget(label, row, 0);
gridLayout->addWidget(edit, row, 1);
}
}

Widget::~Widget()
{
}