Set equal size for all row headers

This commit is contained in:
Michael Mandl 2019-02-02 14:44:01 +01:00
parent 13a6468757
commit baf0cb8dbe
5 changed files with 42 additions and 51 deletions

View file

@ -5,13 +5,13 @@
#include <QDebug>
CheckableTestModel::CheckableTestModel(QObject *parent)
: QAbstractTableModel(parent)
: QAbstractTableModel(parent)
{
}
int CheckableTestModel::rowCount(const QModelIndex &) const
{
return static_cast<int>(m_tests.size());
return static_cast<int>(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<int>(test.size()));
columnCount = std::max(columnCount, static_cast<int>(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)
{

View file

@ -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();

View file

@ -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;
};

View file

@ -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();
}

View file

@ -15,17 +15,7 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTableView" name="pluralTableView">
<attribute name="horizontalHeaderVisible">
<bool>false</bool>
</attribute>
<attribute name="horizontalHeaderDefaultSectionSize">
<number>120</number>
</attribute>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
</widget>
<widget class="QTableView" name="pluralTableView"/>
</item>
</layout>
</widget>