| 1 |
// Copyright (C) 2012 Thorbjørn Lund Martsum - tmartsum[at]gmail.com |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include <QVBoxLayout> |
| 5 |
#include <QTreeWidget> |
| 6 |
#include <QGroupBox> |
| 7 |
#include <QRadioButton> |
| 8 |
#include <QDialog> |
| 9 |
#include <QApplication> |
| 10 |
#include <QHeaderView> |
| 11 |
|
| 12 |
class ExampleDlg : public QDialog |
| 13 |
{ |
| 14 |
Q_OBJECT |
| 15 |
public: |
| 16 |
QVBoxLayout *groupLayout; |
| 17 |
QVBoxLayout *dialogLayout; |
| 18 |
QTreeWidget *treeWidget; |
| 19 |
QGroupBox *groupBox; |
| 20 |
QRadioButton *radioFirstName; |
| 21 |
QRadioButton *radioLastName; |
| 22 |
QRadioButton *radioDeveloperNo; |
| 23 |
QRadioButton *radioTitle; |
| 24 |
|
| 25 |
ExampleDlg() : QDialog(0) |
| 26 |
{ |
| 27 |
dialogLayout = new QVBoxLayout(this); |
| 28 |
treeWidget = new QTreeWidget(this); |
| 29 |
dialogLayout->addWidget(treeWidget); |
| 30 |
|
| 31 |
groupBox = new QGroupBox(this); |
| 32 |
groupLayout = new QVBoxLayout(groupBox); |
| 33 |
radioFirstName = new QRadioButton("First Name", groupBox); |
| 34 |
groupLayout->addWidget(radioFirstName); |
| 35 |
radioLastName = new QRadioButton("Last Name", groupBox); |
| 36 |
groupLayout->addWidget(radioLastName); |
| 37 |
radioDeveloperNo = new QRadioButton("Developer No.", groupBox); |
| 38 |
groupLayout->addWidget(radioDeveloperNo); |
| 39 |
radioTitle = new QRadioButton("Title", groupBox); |
| 40 |
groupLayout->addWidget(radioTitle); |
| 41 |
dialogLayout->addWidget(groupBox); |
| 42 |
|
| 43 |
QStringList item1sl("Barry"); |
| 44 |
item1sl.append("Butter"); |
| 45 |
item1sl.append("12199"); |
| 46 |
item1sl.append("Key Maintainer"); |
| 47 |
QTreeWidgetItem *item1 = new QTreeWidgetItem(treeWidget, item1sl); |
| 48 |
|
| 49 |
QStringList item2sl("Cordon"); |
| 50 |
item2sl.append("Rampsey"); |
| 51 |
item2sl.append("59299"); |
| 52 |
item2sl.append("Maintainer"); |
| 53 |
QTreeWidgetItem *item2 = new QTreeWidgetItem(item1, item2sl); |
| 54 |
|
| 55 |
QStringList item3sl("Samuel le"); |
| 56 |
item3sl.append("Smackson"); |
| 57 |
item3sl.append("708"); |
| 58 |
item3sl.append("Contributer"); |
| 59 |
/* QTreeWidgetItem *item3 = */ new QTreeWidgetItem(item2, item3sl); |
| 60 |
|
| 61 |
QStringList item4sl("Georg"); |
| 62 |
item4sl.append("Ambush"); |
| 63 |
item4sl.append("86999"); |
| 64 |
item4sl.append("Area Maintainer"); |
| 65 |
QTreeWidgetItem *item4 = new QTreeWidgetItem(item1, item4sl); |
| 66 |
|
| 67 |
QStringList item5sl("Arne"); |
| 68 |
item5sl.append("Strassenleger"); |
| 69 |
item5sl.append("338999"); |
| 70 |
item5sl.append("Approver"); |
| 71 |
/* QTreeWidgetItem *item4 =*/ new QTreeWidgetItem(item4, item5sl); |
| 72 |
|
| 73 |
treeWidget->setDragEnabled(true); |
| 74 |
treeWidget->viewport()->setAcceptDrops(true); |
| 75 |
treeWidget->setDragDropMode(QAbstractItemView::InternalMove); |
| 76 |
|
| 77 |
for (int u = 0; u < 12; ++u) { |
| 78 |
const QString username = QString("Anonymous User %1").arg(u + 1); |
| 79 |
QStringList info; |
| 80 |
info << username << username << QString::number(u + 1) << QStringLiteral("Test user"); |
| 81 |
new QTreeWidgetItem(item4, info); |
| 82 |
} |
| 83 |
|
| 84 |
treeWidget->expandAll(); |
| 85 |
treeWidget->setColumnCount(item2sl.size()); |
| 86 |
QStringList itemInfo("First Name"); |
| 87 |
itemInfo.append("Last Name"); |
| 88 |
itemInfo.append("Developer No."); |
| 89 |
// Developer no. could also have been social security number og some other id. |
| 90 |
itemInfo.append("Title"); |
| 91 |
treeWidget->setHeaderLabels(itemInfo); |
| 92 |
radioLastName->setChecked(true); |
| 93 |
|
| 94 |
connect(radioFirstName, SIGNAL(toggled(bool)), this, SLOT(fixDataInTree(bool))); |
| 95 |
connect(radioLastName, SIGNAL(toggled(bool)), this, SLOT(fixDataInTree(bool))); |
| 96 |
connect(radioDeveloperNo, SIGNAL(toggled(bool)), this, SLOT(fixDataInTree(bool))); |
| 97 |
connect(radioTitle, SIGNAL(toggled(bool)), this, SLOT(fixDataInTree(bool))); |
| 98 |
treeWidget->setTreePosition(-1); |
| 99 |
treeWidget->header()->swapSections(0, 1); |
| 100 |
} |
| 101 |
|
| 102 |
protected slots: |
| 103 |
void fixDataInTree(bool checked) |
| 104 |
{ |
| 105 |
if (!checked) |
| 106 |
return; |
| 107 |
int colInTree = 0; // first Name |
| 108 |
if (radioLastName->isChecked()) |
| 109 |
colInTree = 1; |
| 110 |
if (radioDeveloperNo->isChecked()) |
| 111 |
colInTree = 2; |
| 112 |
if (radioTitle->isChecked()) |
| 113 |
colInTree = 3; |
| 114 |
treeWidget->header()->swapSections(0, treeWidget->header()->visualIndex(colInTree)); |
| 115 |
} |
| 116 |
}; |
| 117 |
|
| 118 |
|
| 119 |
int main(int argc, char *argv[]) |
| 120 |
{ |
| 121 |
QApplication app(argc, argv); |
| 122 |
ExampleDlg d; |
| 123 |
d.resize(d.sizeHint() * 3); |
| 124 |
d.show(); |
| 125 |
app.exec(); |
| 126 |
} |
| 127 |
|
| 128 |
#include "main.moc" |