QSqlQueryModel Класс как модель для qml TableView

Приложение запускается, но TableView пуст. База данных открыта и не показывает мне ошибок. Я сделал отдельный класс для подключения к базе данных.

model.cpp

QVariant Model::data(const QModelIndex &index, int role) const {
if (!index.isValid())
    return QVariant();

QString fieldName;
switch (role) {
    case IDRole: fieldName = QStringLiteral("Poems.ID"); break;
    case TitleRole: fieldName = QStringLiteral("Poems.Title"); break;
    case PoemRole: fieldName = QStringLiteral("Poems.Poem"); break;
    case GrpRole: fieldName = QStringLiteral("Poems.Grp"); break;
}

if (!this->record().isGenerated(fieldName))
    return QVariant();
else {
    QModelIndex item = indexInQuery(index);
    if ( !this->query().seek(item.row()) )
        return QVariant();
    return this->query().value(fieldName);
}
return QVariant();
}
QHash<int, QByteArray> Model::roleNames() const {
QHash<int, QByteArray> roles;
roles[IDRole] = "binIdRole";
roles[TitleRole] = "bindTitleRole";
roles[PoemRole] = "bindPoemRole";
roles[GrpRole] = "bindGrpRole";
return roles;
}

main.qml

TableView {
    anchors.fill: parent
    TableViewColumn {
        role: "binIdRole"
        title: "Id"
    }
    model: myModel
}

0 ответов

Другие вопросы по тегам