Skip to content

Instantly share code, notes, and snippets.

@SC-One
Created May 17, 2023 14:02
Show Gist options
  • Save SC-One/10f515261d20fdf41db0237ea51ece6f to your computer and use it in GitHub Desktop.
Save SC-One/10f515261d20fdf41db0237ea51ece6f to your computer and use it in GitHub Desktop.
How subclassing QAbstractTableModel in qt
class FooTableModel : public QAbstractTableModel {
Q_OBJECT
public:
FooTableModel(QObject *parent = nullptr);
// readonly tables
Qt::ItemFlags flags(const QModelIndex &index)
const override; // editable can return: Qt::ItemIsEditable
QVariant data(const QModelIndex &index,
int role = Qt::DisplayRole) const override;
QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QHash<int, QByteArray> roleNames() const override;
// editable model have methods(we dont need them now , and even later):
bool setData(const QModelIndex &index, const QVariant &value,
int role = Qt::EditRole) override;
bool setHeaderData(int section, Qt::Orientation orientation,
const QVariant &value, int role = Qt::EditRole) override;
// resizeAble model have methods:
bool insertRows(int row, int count,
const QModelIndex &parent = QModelIndex()) override;
bool removeRows(int row, int count,
const QModelIndex &parent = QModelIndex()) override;
bool insertColumns(int column, int count,
const QModelIndex &parent = QModelIndex()) override;
bool removeColumns(int column, int count,
const QModelIndex &parent = QModelIndex()) override;
private:
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment