I gave it a try, check out the code below.
It's absolutely not perfect. In fact, I think the most correct way to do this is to create a QLayout subclass.
Screenshot attached below
Edit:
It works by keeping a list of groups and items.
First clear the layout from any widget or layout item.
Then go through each group.
In each group, add all the items to the layout, keeping below the maximum amount of columns
Then, when every item of a group is added add labels, keeping an eye on the span of the items for this group in the rows.
It might not be exactly like you want, but I think you can take it from here :-)
Header
#ifndef SPECIALLAYOUT_H
#define SPECIALLAYOUT_H
#include <QGridLayout>
#include <QStringList>
#include <QString>
#include <QMultiMap>
#include <QWidget>
{
Q_OBJECT
public:
explicit SpecialLayout
(QWidget *parent
= 0);
void addGroup
(const QString &groupName
);
void addItemWidget
(const QString &inGroup
);
void setMaximumColumns(int columns);
signals:
public slots:
protected:
void rebuildLayout();
private:
QMultiMap<QString, QString> m_groupWidgetMap;
int m_maximumColumnCount;
};
#endif // SPECIALLAYOUT_H
#ifndef SPECIALLAYOUT_H
#define SPECIALLAYOUT_H
#include <QGridLayout>
#include <QStringList>
#include <QString>
#include <QMultiMap>
#include <QWidget>
class SpecialLayout : public QGridLayout
{
Q_OBJECT
public:
explicit SpecialLayout(QWidget *parent = 0);
void addGroup(const QString &groupName);
void addItemWidget(const QString &inGroup);
void setMaximumColumns(int columns);
QStringList groupNames() const;
signals:
public slots:
protected:
void rebuildLayout();
private:
QStringList m_groupNames;
QMultiMap<QString, QString> m_groupWidgetMap;
int m_maximumColumnCount;
};
#endif // SPECIALLAYOUT_H
To copy to clipboard, switch view to plain text mode
Implementation
#include "speciallayout.h"
#include <QLabel>
#include <QSpacerItem>
#include <QSizePolicy>
SpecialLayout
::SpecialLayout(QWidget *parent
) :{
m_maximumColumnCount = 10;
}
void SpecialLayout
::addGroup(const QString &groupName
) {
if (m_groupNames.contains(groupName))
return;
m_groupNames.append(groupName);
}
void SpecialLayout
::addItemWidget(const QString &inGroup
) {
if (!m_groupNames.contains(inGroup))
return;
m_groupWidgetMap.insert(inGroup, "Item");
rebuildLayout();
}
{
return m_groupNames;
}
void SpecialLayout::setMaximumColumns(int columns)
{
m_maximumColumnCount = columns;
}
void SpecialLayout::rebuildLayout()
{
while ((item = takeAt(0))) {
if (item->widget())
delete item->widget();
delete item;
}
int currentColumn = 0;
int currentRow = 0;
int widgetColumn = 0;
int widgetRow = 0;
int columnCount = 0;
foreach
(QString group, m_groupNames
) {
if (!m_groupWidgetMap.values(group).isEmpty()) {
widgetColumn = currentColumn;
widgetRow = currentRow;
foreach
(QString string, m_groupWidgetMap.
values(group
)) { addWidget(itemWidget, widgetRow + 1, widgetColumn);
++widgetColumn;
if (widgetColumn > m_maximumColumnCount - 1) {
widgetColumn = 0;
widgetRow += 2;
}
}
columnCount = m_groupWidgetMap.values(group).count();
int columnSpan;
while (columnCount > 0) {
columnSpan = qMin(m_maximumColumnCount - currentColumn, columnCount);
addWidget(label, currentRow, currentColumn, 1, columnSpan);
columnCount -= columnSpan;
currentColumn += columnSpan;
if (currentColumn > m_maximumColumnCount - 1) {
currentColumn = 0;
currentRow += 2;
}
}
}
}
}
#include "speciallayout.h"
#include <QLabel>
#include <QSpacerItem>
#include <QSizePolicy>
SpecialLayout::SpecialLayout(QWidget *parent) :
QGridLayout(parent)
{
m_maximumColumnCount = 10;
}
void SpecialLayout::addGroup(const QString &groupName)
{
if (m_groupNames.contains(groupName))
return;
m_groupNames.append(groupName);
}
void SpecialLayout::addItemWidget(const QString &inGroup)
{
if (!m_groupNames.contains(inGroup))
return;
m_groupWidgetMap.insert(inGroup, "Item");
rebuildLayout();
}
QStringList SpecialLayout::groupNames() const
{
return m_groupNames;
}
void SpecialLayout::setMaximumColumns(int columns)
{
m_maximumColumnCount = columns;
}
void SpecialLayout::rebuildLayout()
{
QLayoutItem *item;
while ((item = takeAt(0))) {
if (item->widget())
delete item->widget();
delete item;
}
int currentColumn = 0;
int currentRow = 0;
int widgetColumn = 0;
int widgetRow = 0;
int columnCount = 0;
foreach(QString group, m_groupNames) {
if (!m_groupWidgetMap.values(group).isEmpty()) {
widgetColumn = currentColumn;
widgetRow = currentRow;
foreach(QString string, m_groupWidgetMap.values(group)) {
QLabel *itemWidget = new QLabel("<b>" + string + "</b>");
addWidget(itemWidget, widgetRow + 1, widgetColumn);
++widgetColumn;
if (widgetColumn > m_maximumColumnCount - 1) {
widgetColumn = 0;
widgetRow += 2;
}
}
columnCount = m_groupWidgetMap.values(group).count();
int columnSpan;
while (columnCount > 0) {
columnSpan = qMin(m_maximumColumnCount - currentColumn, columnCount);
QLabel *label = new QLabel(group);
addWidget(label, currentRow, currentColumn, 1, columnSpan);
columnCount -= columnSpan;
currentColumn += columnSpan;
if (currentColumn > m_maximumColumnCount - 1) {
currentColumn = 0;
currentRow += 2;
}
}
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks