Added a checkable item class to genus model

pull/12/head
mandlm 2018-05-22 08:22:46 +02:00
parent de9518ae43
commit 59ff9712d8
2 changed files with 58 additions and 2 deletions

View File

@ -19,11 +19,21 @@ QVariant GenusModel::data(const QModelIndex &index, int role) const
{ {
if (role == Qt::DisplayRole) if (role == Qt::DisplayRole)
{ {
if (index.row() == 0 && m_tiere.find(index.column()) != m_tiere.end())
{
return m_tiere.find(index.column())->second.getText().c_str();
}
return "Hello"; return "Hello";
} }
if (role == Qt::CheckStateRole) if (role == Qt::CheckStateRole)
{ {
if (index.row() == 0 && m_tiere.find(index.column()) != m_tiere.end())
{
return m_tiere.find(index.column())->second.isChecked() ? Qt::Checked : Qt::Unchecked;
}
return Qt::Unchecked; return Qt::Unchecked;
} }
@ -39,12 +49,21 @@ Qt::ItemFlags GenusModel::flags(const QModelIndex &index) const
bool GenusModel::setData( bool GenusModel::setData(
const QModelIndex &index, const QVariant &value, int role) const QModelIndex &index, const QVariant &value, int role)
{ {
if (role != Qt::EditRole) if (role != Qt::CheckStateRole)
{ {
return QAbstractTableModel::setData(index, value, role); return QAbstractTableModel::setData(index, value, role);
} }
else
{
if (index.row() == 0 && m_tiere.find(index.column()) != m_tiere.end())
{
m_tiere.find(index.column())->second.setState(value.toBool());
return true; return true;
}
}
return false;
} }
QVariant GenusModel::headerData( QVariant GenusModel::headerData(

View File

@ -1,5 +1,35 @@
#pragma once #pragma once
#include <string>
class CheckableItem
{
private:
bool m_checked = false;
std::string m_text;
public:
CheckableItem(const std::string &text)
: m_text(text)
{
}
std::string getText() const
{
return m_text;
}
bool isChecked() const
{
return m_checked;
}
void setState(bool checked)
{
m_checked = checked;
}
};
#include <QAbstractTableModel> #include <QAbstractTableModel>
#include <QJsonObject> #include <QJsonObject>
@ -7,6 +37,13 @@ class GenusModel : public QAbstractTableModel
{ {
Q_OBJECT Q_OBJECT
private:
std::map<size_t, CheckableItem> m_tiere =
{
{0, {"null"}},
{1, {"eins"}}
};
public: public:
GenusModel(QObject *parent); GenusModel(QObject *parent);
int rowCount(const QModelIndex &parent = QModelIndex()) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override;