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

@ -67,8 +67,7 @@ Qt::ItemFlags CheckableTestModel::flags(const QModelIndex &index) const
return Qt::NoItemFlags; return Qt::NoItemFlags;
} }
bool CheckableTestModel::setData( bool CheckableTestModel::setData(const QModelIndex &index, const QVariant &value, int role)
const QModelIndex &index, const QVariant &value, int role)
{ {
if (!isValidIndex(index)) if (!isValidIndex(index))
{ {
@ -93,16 +92,31 @@ bool CheckableTestModel::setData(
return false; return false;
} }
QVariant CheckableTestModel::headerData( QVariant CheckableTestModel::headerData(int section, Qt::Orientation orientation, int role) const
int section, Qt::Orientation orientation, int role) const
{ {
if (role == Qt::DisplayRole && orientation == Qt::Vertical) switch (orientation)
{
case Qt::Vertical:
{
switch (role)
{
case Qt::DisplayRole:
{ {
if (section < m_tests.size()) if (section < m_tests.size())
{ {
return m_tests.at(section).name(); 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);
} }
@ -151,8 +165,7 @@ CheckableItems &CheckableTestModel::getItems(const QModelIndex &index)
throw std::runtime_error("invalid index"); throw std::runtime_error("invalid index");
} }
const CheckableItems &CheckableTestModel::getItems( const CheckableItems &CheckableTestModel::getItems(const QModelIndex &index) const
const QModelIndex &index) const
{ {
if (index.row() < m_tests.size()) if (index.row() < m_tests.size())
{ {

View file

@ -5,21 +5,11 @@
PluralModel::PluralModel(QObject *parent) PluralModel::PluralModel(QObject *parent)
: CheckableTestModel(parent) : CheckableTestModel(parent)
{ {
m_tests = {{"", m_tests = {{"Plural",
{"Fisch /-e/", "Banane /-n/", "Bonbon /-s/", "Ei /-er/", "Eimer /-ø/", {"Fisch /-e/", "Banane /-n/", "Bonbon /-s/", "Ei /-er/", "Eimer /-ø/",
"Korn UML+/-er/", "Nuss UML+/-e/", "Bär /-en/", "Apfel UML"}}}; "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) void PluralModel::read(const ESGRAF48::PluralModel &model)
{ {
auto &testItems = m_tests.at(0).items(); auto &testItems = m_tests.at(0).items();

View file

@ -10,9 +10,6 @@ class PluralModel : public CheckableTestModel
public: public:
PluralModel(QObject *parent); PluralModel(QObject *parent);
QVariant data(
const QModelIndex &index, int role = Qt::DisplayRole) const override;
void read(const ESGRAF48::PluralModel &model); void read(const ESGRAF48::PluralModel &model);
void write(ESGRAF48::PluralModel &model) const; void write(ESGRAF48::PluralModel &model) const;
}; };

View file

@ -8,6 +8,8 @@ PluralWidget::PluralWidget(QWidget *parent)
, ui(new Ui::PluralWidget) , ui(new Ui::PluralWidget)
{ {
ui->setupUi(this); ui->setupUi(this);
ui->pluralTableView->horizontalHeader()->hide();
} }
PluralWidget::~PluralWidget() PluralWidget::~PluralWidget()
@ -18,5 +20,4 @@ PluralWidget::~PluralWidget()
void PluralWidget::setModel(PluralModel *model) void PluralWidget::setModel(PluralModel *model)
{ {
ui->pluralTableView->setModel(model); ui->pluralTableView->setModel(model);
ui->pluralTableView->resizeColumnsToContents();
} }

View file

@ -15,17 +15,7 @@
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<item> <item>
<widget class="QTableView" name="pluralTableView"> <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>
</item> </item>
</layout> </layout>
</widget> </widget>