From baf0cb8dbe14b86bb186c72e555117a76ea89700 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Sat, 2 Feb 2019 14:44:01 +0100 Subject: [PATCH] Set equal size for all row headers --- .../CheckableTestModel/CheckableTestModel.cpp | 63 +++++++++++-------- source/SubTests/Plural/PluralModel.cpp | 12 +--- source/SubTests/Plural/PluralModel.h | 3 - source/SubTests/Plural/PluralWidget.cpp | 3 +- source/SubTests/Plural/PluralWidget.ui | 12 +--- 5 files changed, 42 insertions(+), 51 deletions(-) diff --git a/source/CheckableTestModel/CheckableTestModel.cpp b/source/CheckableTestModel/CheckableTestModel.cpp index 493bd76..9c46e03 100644 --- a/source/CheckableTestModel/CheckableTestModel.cpp +++ b/source/CheckableTestModel/CheckableTestModel.cpp @@ -5,13 +5,13 @@ #include CheckableTestModel::CheckableTestModel(QObject *parent) - : QAbstractTableModel(parent) + : QAbstractTableModel(parent) { } int CheckableTestModel::rowCount(const QModelIndex &) const { - return static_cast(m_tests.size()); + return static_cast(m_tests.size()); } int CheckableTestModel::columnCount(const QModelIndex &) const @@ -20,7 +20,7 @@ int CheckableTestModel::columnCount(const QModelIndex &) const for (const auto &test : m_tests) { - columnCount = std::max(columnCount, static_cast(test.size())); + columnCount = std::max(columnCount, static_cast(test.size())); } return columnCount; @@ -37,17 +37,17 @@ QVariant CheckableTestModel::data(const QModelIndex &index, int role) const { auto &item = getItem(index); - switch (role) - { - case Qt::DisplayRole: - { - return item.getText().c_str(); - } - case Qt::CheckStateRole: - { - return item.isChecked() ? Qt::Checked : Qt::Unchecked; - } - } + switch (role) + { + case Qt::DisplayRole: + { + return item.getText().c_str(); + } + case Qt::CheckStateRole: + { + return item.isChecked() ? Qt::Checked : Qt::Unchecked; + } + } } catch (std::runtime_error &e) { @@ -67,8 +67,7 @@ Qt::ItemFlags CheckableTestModel::flags(const QModelIndex &index) const return Qt::NoItemFlags; } -bool CheckableTestModel::setData( - const QModelIndex &index, const QVariant &value, int role) +bool CheckableTestModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (!isValidIndex(index)) { @@ -93,18 +92,33 @@ bool CheckableTestModel::setData( return false; } -QVariant CheckableTestModel::headerData( - int section, Qt::Orientation orientation, int role) const +QVariant CheckableTestModel::headerData(int section, Qt::Orientation orientation, int role) const { - if (role == Qt::DisplayRole && orientation == Qt::Vertical) + switch (orientation) { - if (section < m_tests.size()) + case Qt::Vertical: { - return m_tests.at(section).name(); + switch (role) + { + case Qt::DisplayRole: + { + if (section < m_tests.size()) + { + return m_tests.at(section).name(); + } + } + case Qt::SizeHintRole: + { + return QSize(200, 0); + } + } + break; } + default: + break; } - return QAbstractTableModel::headerData(section, orientation, role); + return QAbstractTableModel::headerData(section, orientation, role); } void CheckableTestModel::write(QJsonObject &json) const @@ -151,8 +165,7 @@ CheckableItems &CheckableTestModel::getItems(const QModelIndex &index) throw std::runtime_error("invalid index"); } -const CheckableItems &CheckableTestModel::getItems( - const QModelIndex &index) const +const CheckableItems &CheckableTestModel::getItems(const QModelIndex &index) const { if (index.row() < m_tests.size()) { @@ -186,7 +199,7 @@ const CheckableItem &CheckableTestModel::getItem(const QModelIndex &index) const unsigned int CheckableTestModel::getPoints() const { - unsigned int points = 0; + unsigned int points = 0; for (const auto &test : m_tests) { diff --git a/source/SubTests/Plural/PluralModel.cpp b/source/SubTests/Plural/PluralModel.cpp index 0ade859..dcd0959 100644 --- a/source/SubTests/Plural/PluralModel.cpp +++ b/source/SubTests/Plural/PluralModel.cpp @@ -5,21 +5,11 @@ PluralModel::PluralModel(QObject *parent) : CheckableTestModel(parent) { - m_tests = {{"", + m_tests = {{"Plural", {"Fisch /-e/", "Banane /-n/", "Bonbon /-s/", "Ei /-er/", "Eimer /-ø/", "Korn UML+/-er/", "Nuss UML+/-e/", "Bär /-en/", "Apfel UML"}}}; } -QVariant PluralModel::data(const QModelIndex &index, int role) const -{ - if (role == Qt::SizeHintRole) - { - return QSize(180, 0); - } - - return CheckableTestModel::data(index, role); -} - void PluralModel::read(const ESGRAF48::PluralModel &model) { auto &testItems = m_tests.at(0).items(); diff --git a/source/SubTests/Plural/PluralModel.h b/source/SubTests/Plural/PluralModel.h index cadeae9..ef1381f 100644 --- a/source/SubTests/Plural/PluralModel.h +++ b/source/SubTests/Plural/PluralModel.h @@ -10,9 +10,6 @@ class PluralModel : public CheckableTestModel public: PluralModel(QObject *parent); - QVariant data( - const QModelIndex &index, int role = Qt::DisplayRole) const override; - void read(const ESGRAF48::PluralModel &model); void write(ESGRAF48::PluralModel &model) const; }; diff --git a/source/SubTests/Plural/PluralWidget.cpp b/source/SubTests/Plural/PluralWidget.cpp index 4776e52..cf78939 100644 --- a/source/SubTests/Plural/PluralWidget.cpp +++ b/source/SubTests/Plural/PluralWidget.cpp @@ -8,6 +8,8 @@ PluralWidget::PluralWidget(QWidget *parent) , ui(new Ui::PluralWidget) { ui->setupUi(this); + + ui->pluralTableView->horizontalHeader()->hide(); } PluralWidget::~PluralWidget() @@ -18,5 +20,4 @@ PluralWidget::~PluralWidget() void PluralWidget::setModel(PluralModel *model) { ui->pluralTableView->setModel(model); - ui->pluralTableView->resizeColumnsToContents(); } diff --git a/source/SubTests/Plural/PluralWidget.ui b/source/SubTests/Plural/PluralWidget.ui index ec1230f..7e78ce4 100644 --- a/source/SubTests/Plural/PluralWidget.ui +++ b/source/SubTests/Plural/PluralWidget.ui @@ -15,17 +15,7 @@ - - - false - - - 120 - - - false - - +