C++ (Qt)QObject obj; // должно выдать ошибку
C++ (Qt)#pragma once #include <QAbstractTableModel> #include "Entities/PassedTestPreview/PassedTestPreview.h" class QTimer;class PassedTestPreview;class PassedTestsService; class PassedTestsModel : public QAbstractTableModel { Q_OBJECT public: // :: Lifecycle :: PassedTestsModel(QObject *parent = nullptr); // :: QAbstractTableModel :: int rowCount(const QModelIndex &parent) const override; int columnCount(const QModelIndex &parent) const override; QVariant data(const QModelIndex &index, int role) const override; QVariant headerData(int section, Qt::Orientation orientation, int role) const; // :: Accessors :: QList<PassedTestPreview> getPassedTestPreviews() const; void setPassedTestPreviews(const QList<PassedTestPreview> &previews); public slots: void startUpdating() const; void stopUpdating() const; private: void initService(); void initTimer(); void emitDataChanged(); QList<PassedTestPreview> m_previews; QTimer *m_timer; PassedTestsService *m_service;};
C++ (Qt)#include "PassedTestsModel.h" #include <QTimer> #include "Entities/PassedTestPreview/PassedTestPreview.h" #include "../Service/PassedTestsService.h" // :: Constants :: enum ColumnIndex { NAME_COLUMN_INDEX, NUMBER_OF_PASSES_COLUMN_INDEX, NUMBER_OF_COLUMNS}; const QStringList kHorizontalHeaders { "Название теста", "Пройден"}; const int UPDATE_INTERVAL = 5000; // :: Lifecycle :: PassedTestsModel::PassedTestsModel(QObject *parent/*= nullptr*/) : QAbstractTableModel(parent){ initService(); initTimer(); connect(m_timer, &QTimer::timeout, m_service, &PassedTestsService::getPreviews); startUpdating();} // :: QAbstractTableModel :: int PassedTestsModel::rowCount(const QModelIndex &) const { return m_previews.size();} int PassedTestsModel::columnCount(const QModelIndex &) const { return NUMBER_OF_COLUMNS;} QVariant PassedTestsModel::data(const QModelIndex &index, int role) const { if (index.isValid() && role == Qt::DisplayRole) { auto preview = m_previews[index.row()]; switch (index.column()) { case NAME_COLUMN_INDEX: return preview.getName(); case NUMBER_OF_PASSES_COLUMN_INDEX: return preview.getNumberOfPasses(); } } return QVariant();} QVariant PassedTestsModel::headerData(int section, Qt::Orientation orientation, int role) const { if (role == Qt::DisplayRole) { if (orientation == Qt::Horizontal) { return kHorizontalHeaders[section]; } else { return section + 1; } } return QVariant();} // :: Public accessors ::// :: Passed test previews ::QList<PassedTestPreview> PassedTestsModel::getPassedTestPreviews() const { return m_previews;}void PassedTestsModel::setPassedTestPreviews(const QList<PassedTestPreview> &previews) { m_previews = previews; emitDataChanged();} // :: Public slots :: void PassedTestsModel::startUpdating() const { m_service->getPreviews(); m_timer->start();} void PassedTestsModel::stopUpdating() const { m_timer->stop();} // :: Private methods :: inlinevoid PassedTestsModel::initService() { m_service = new PassedTestsService(this); connect(m_service, &PassedTestsService::previewsGot, this, &PassedTestsModel::setPassedTestPreviews);} inlinevoid PassedTestsModel::initTimer() { m_timer = new QTimer(this); m_timer->setInterval(UPDATE_INTERVAL);} inlinevoid PassedTestsModel::emitDataChanged() { auto topLeft = index(0, 0); auto bottomRight = index(m_previews.size() - 1, NUMBER_OF_COLUMNS - 1); emit dataChanged(topLeft, bottomRight);}