Software development challenges around index creation for data models
-
@elfring said in Software development challenges around index creation for data models:
This design approach sounds very promising. How should data accesses be redirected to the existing container object here?
MyModel::MyModel(QObject * parent) : QAbstractTableModel(parent), dataSource(nullptr) { } void MyModel::setDataSource(MyDataSource * source) { if (dataSource) { QObject::disconnect(this, nullptr, dataSource, nullptr); QObject::disconnect(dataSource, nullptr, this, nullptr); } dataSource = source; QObject::connect(dataSource, &MyDataSource::dataChangeStarted, this, &MyModel::beginResetModel); QObject::connect(dataSource, &MyDataSource::dataChangeFinished, this, &MyModel::endResetModel); } QVariant MyModel::headerData(int section, Qt::Orientation orientation, int role) const { if (role != Qt::DisplayRole || orientation != Qt::Horizontal || section >= dataSource->columnCount()) return QVariant(); Q_ASSERT(dataSource); return dataSource->columnName(section); } int MyModel::rowCount(const QModelIndex &) const { Q_ASSERT(dataSource); return dataSource->rowCount(); } int MyModel::columnCount(const QModelIndex &) const { Q_ASSERT(dataSource); return dataSource->columnCount(); } QVariant MyModel::data(const QModelIndex & index, int role) const { Q_ASSERT(dataSource); if (!index.isValid() || role != Qt::DisplayRole) return QVariant(); return dataSource->value(index.row(), index.column()); }
and of course the corresponding interface:
class MyDataSource : public QObject { Q_OBJECT Q_DISABLE_COPY(MyDataSource) public: MyDataSource(QObject * = nullptr); virtual int rowCount() const = 0; virtual int columnCount() const = 0; virtual QString columnName(int) const = 0; virtual QVariant value(int, int) const = 0; virtual QString format(const QVariant &, int) const; signals: void changed(); void dataChangeStarted(); void dataChangeFinished(); protected slots: virtual void reloadData() = 0; };
wrote on 12 Oct 2018, 06:32 last edited byand of course the corresponding interface:
Will your data source provide homogenous items for this model?
-
and of course the corresponding interface:
Will your data source provide homogenous items for this model?
Moderatorswrote on 12 Oct 2018, 06:45 last edited by kshegunov 10 Dec 2018, 06:45@elfring said in Software development challenges around index creation for data models:
Will your data source provide homogenous items for this model?
Do you mean the data types? If so, then no, the data types are different for the different columns of the table.
-
@elfring said in Software development challenges around index creation for data models:
Will your data source provide homogenous items for this model?
Do you mean the data types? If so, then no, the data types are different for the different columns of the table.
wrote on 12 Oct 2018, 07:11 last edited by…, the data types are different for the different columns of the table.
Will your data source work without hierarchies then?
-
@elfring said in Software development challenges around index creation for data models:
This design approach sounds very promising. How should data accesses be redirected to the existing container object here?
MyModel::MyModel(QObject * parent) : QAbstractTableModel(parent), dataSource(nullptr) { } void MyModel::setDataSource(MyDataSource * source) { if (dataSource) { QObject::disconnect(this, nullptr, dataSource, nullptr); QObject::disconnect(dataSource, nullptr, this, nullptr); } dataSource = source; QObject::connect(dataSource, &MyDataSource::dataChangeStarted, this, &MyModel::beginResetModel); QObject::connect(dataSource, &MyDataSource::dataChangeFinished, this, &MyModel::endResetModel); } QVariant MyModel::headerData(int section, Qt::Orientation orientation, int role) const { if (role != Qt::DisplayRole || orientation != Qt::Horizontal || section >= dataSource->columnCount()) return QVariant(); Q_ASSERT(dataSource); return dataSource->columnName(section); } int MyModel::rowCount(const QModelIndex &) const { Q_ASSERT(dataSource); return dataSource->rowCount(); } int MyModel::columnCount(const QModelIndex &) const { Q_ASSERT(dataSource); return dataSource->columnCount(); } QVariant MyModel::data(const QModelIndex & index, int role) const { Q_ASSERT(dataSource); if (!index.isValid() || role != Qt::DisplayRole) return QVariant(); return dataSource->value(index.row(), index.column()); }
and of course the corresponding interface:
class MyDataSource : public QObject { Q_OBJECT Q_DISABLE_COPY(MyDataSource) public: MyDataSource(QObject * = nullptr); virtual int rowCount() const = 0; virtual int columnCount() const = 0; virtual QString columnName(int) const = 0; virtual QVariant value(int, int) const = 0; virtual QString format(const QVariant &, int) const; signals: void changed(); void dataChangeStarted(); void dataChangeFinished(); protected slots: virtual void reloadData() = 0; };
wrote on 12 Oct 2018, 08:57 last edited by@kshegunov said in Software development challenges around index creation for data models:
QObject::connect(dataSource, &MyDataSource::dataChangeStarted, this, &MyModel::beginResetModel);
QObject::connect(dataSource, &MyDataSource::dataChangeFinished, this, &MyModel::endResetModel);I know you can do better than this mate! I know you have to create and connect a gazillion signals but at least you don't build everything from scratch
-
@kshegunov said in Software development challenges around index creation for data models:
QObject::connect(dataSource, &MyDataSource::dataChangeStarted, this, &MyModel::beginResetModel);
QObject::connect(dataSource, &MyDataSource::dataChangeFinished, this, &MyModel::endResetModel);I know you can do better than this mate! I know you have to create and connect a gazillion signals but at least you don't build everything from scratch
Moderatorswrote on 12 Oct 2018, 09:10 last edited by kshegunov 10 Dec 2018, 09:14@VRonin said in Software development challenges around index creation for data models:
I know you can do better than this mate! I know you have to create and connect a gazillion signals but at least you don't build everything from scratch
Actually I can't in this case. I know you want me to emit the row/columns changed and the
***Inserted
/***Deleted
signals, but it simply isn't applicable in this particular case.PS.
Here the data comes from a longJOIN
of tables and I'd rather emitmodelReset
after I had processed all the peculiarities, than to do a fetch from the database on each displayed cell/row. -
…, the data types are different for the different columns of the table.
Will your data source work without hierarchies then?
@elfring said in Software development challenges around index creation for data models:
Will your data source work without hierarchies then?
I don't follow. This is a table model, there's no hierarchy here.
21/26