File indexing completed on 2024-12-08 09:42:05
0001 /* 0002 SPDX-FileCopyrightText: 2020 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com> 0003 SPDX-FileContributor: David Faure <david.faure@kdab.com> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include <KRearrangeColumnsProxyModel> 0009 #include <QApplication> 0010 #include <QStandardItemModel> 0011 #include <QTimer> 0012 #include <QTreeView> 0013 0014 // Prepares one row for a QStandardItemModel 0015 inline QList<QStandardItem *> makeStandardItems(const QStringList &texts) 0016 { 0017 QList<QStandardItem *> items; 0018 items.reserve(texts.count()); 0019 for (const QString &txt : texts) { 0020 items << new QStandardItem(txt); 0021 } 0022 return items; 0023 } 0024 0025 int main(int argc, char **argv) 0026 { 0027 QApplication app(argc, argv); 0028 0029 QStandardItemModel source; 0030 source.insertRow(0, makeStandardItems({QStringLiteral("A0"), QStringLiteral("B0"), QStringLiteral("C0"), QStringLiteral("D0")})); 0031 source.insertRow(1, makeStandardItems({QStringLiteral("A1"), QStringLiteral("B1"), QStringLiteral("C1"), QStringLiteral("D1")})); 0032 source.insertRow(2, makeStandardItems({QStringLiteral("A2"), QStringLiteral("B2"), QStringLiteral("C2"), QStringLiteral("D2")})); 0033 source.setHorizontalHeaderLabels({QStringLiteral("H1"), QStringLiteral("H2"), QStringLiteral("H3"), QStringLiteral("H4")}); 0034 0035 KRearrangeColumnsProxyModel pm; 0036 pm.setSourceColumns(QVector<int>{2, 3, 1, 0}); 0037 pm.setSourceModel(&source); 0038 0039 QTreeView treeView; 0040 treeView.setModel(&pm); 0041 treeView.show(); 0042 0043 QTimer::singleShot(500, &pm, [&]() { 0044 pm.setSourceColumns(QVector<int>{2, 1, 0, 3}); 0045 }); 0046 0047 return app.exec(); 0048 }