From 49b2e231c0d01d525f0906b37faedddc0485d979 Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Thu, 5 Nov 2015 21:39:21 +0100 Subject: [PATCH] Automatic column names. --- core/autotablemodel.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/core/autotablemodel.h b/core/autotablemodel.h index d399f7c..3586d7c 100644 --- a/core/autotablemodel.h +++ b/core/autotablemodel.h @@ -95,6 +95,30 @@ public: private: QList > m_list; + + // QAbstractItemModel interface +public: + QVariant headerData(int section, Qt::Orientation orientation, int role) const + { + if (role != Qt::DisplayRole) { + return QVariant(); + } + + if (orientation == Qt::Horizontal) { + QObject *entity = (QObject*)new T(); + + for (int i = 0; i < entity->metaObject()->propertyCount(); i++) { + if (i == section) { + QString colName(entity->metaObject()->property(i + 1).name()); + return tr(colName.toStdString().c_str()); + } + } + + delete entity; + } + + return QVariant(section + 1); + } }; #endif // ODBTABLEMODEL_H