|
9 | 9 | StrftimeChooserWidget::StrftimeChooserWidget(QWidget* parent) |
10 | 10 | : QWidget(parent) |
11 | 11 | { |
| 12 | + const quint8 MAXCOLUMNS = 2; |
12 | 13 | auto* layout = new QGridLayout(this); |
13 | | - auto k = m_buttonData.keys(); |
14 | | - int middle = k.length() / 2; |
15 | | - // add the buttons in 2 columns (they need to be even) |
16 | | - for (int i = 0; i < 2; i++) { |
17 | | - for (int j = 0; j < middle; j++) { |
18 | | - QString key = k.last(); |
19 | | - k.pop_back(); |
20 | | - QString variable = m_buttonData.value(key); |
21 | | - auto* button = new QPushButton(this); |
22 | | - button->setText(tr(key.toStdString().data())); |
23 | | - button->setToolTip(variable); |
24 | | - button->setSizePolicy(QSizePolicy::Expanding, |
25 | | - QSizePolicy::Expanding); |
26 | | - button->setMinimumHeight(25); |
27 | | - layout->addWidget(button, j, i); |
28 | | - connect(button, &QPushButton::clicked, this, [variable, this]() { |
29 | | - emit variableEmitted(variable); |
30 | | - }); |
| 14 | + quint8 row = 0; |
| 15 | + quint8 col = 0; |
| 16 | + QMapIterator<QString, QString> iterator(m_buttonData); |
| 17 | + |
| 18 | + while (iterator.hasNext()) { |
| 19 | + iterator.next(); |
| 20 | + |
| 21 | + QString variable = iterator.value(); |
| 22 | + auto* button = new QPushButton(this); |
| 23 | + button->setText(tr(iterator.key().toStdString().data())); |
| 24 | + button->setToolTip(variable); |
| 25 | + button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
| 26 | + button->setMinimumHeight(25); |
| 27 | + layout->addWidget(button, row, col); |
| 28 | + connect(button, &QPushButton::clicked, this, [variable, this]() { |
| 29 | + emit variableEmitted(variable); |
| 30 | + }); |
| 31 | + |
| 32 | + col++; |
| 33 | + if (col >= MAXCOLUMNS) { |
| 34 | + row++; |
| 35 | + col = 0; |
31 | 36 | } |
32 | 37 | } |
| 38 | + |
33 | 39 | setLayout(layout); |
34 | 40 | } |
35 | 41 |
|
|
0 commit comments