More description to help understand the case.

I want to populate a tableview with user data stored inside a SQLite DB. In the main.c I connect to DB and register the CredentialsModel as a Type:

Qt Code:
  1. #include <QGuiApplication>
  2. #include <QQmlApplicationEngine>
  3. #include <QQmlContext>
  4. #include <QStandardPaths>
  5. #include <QSqlDatabase>
  6. #include <QSqlError>
  7. #include <QtQml>
  8.  
  9. #include "sqlcredentialsmodel.h"
  10.  
  11. static void connectToDatabase()
  12. {
  13. QSqlDatabase database = QSqlDatabase::database();
  14. if (!database.isValid()) {
  15. database = QSqlDatabase::addDatabase("QSQLITE");
  16. if (!database.isValid())
  17. qFatal("Cannot add database: %s", qPrintable(database.lastError().text()));
  18. }
  19.  
  20. const QDir writeDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
  21. if (!writeDir.mkpath("."))
  22. qFatal("Failed to create writable directory at %s", qPrintable(writeDir.absolutePath()));
  23.  
  24. // Ensure that we have a writable location on all devices.
  25. const QString fileName = writeDir.absolutePath() + "/chat-database.sqlite3";
  26. qDebug()<< "file: "<<fileName;
  27. // When using the SQLite driver, open() will create the SQLite database if it doesn't exist.
  28. database.setDatabaseName(fileName);
  29. if (!database.open()) {
  30. qFatal("Cannot open database: %s", qPrintable(database.lastError().text()));
  31. QFile::remove(fileName);
  32. }
  33. }
  34.  
  35. int main(int argc, char *argv[])
  36. {
  37. QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  38.  
  39. QGuiApplication app(argc, argv);
  40.  
  41. connectToDatabase();
  42.  
  43. QQmlApplicationEngine engine;
  44. qmlRegisterType<SqlCredentialsModel>("credentials", 1, 0, "SqlCredentialsModel");
  45.  
  46. const QUrl url(QStringLiteral("qrc:/main.qml"));
  47. QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
  48. &app, [url](QObject *obj, const QUrl &objUrl) {
  49. if (!obj && url == objUrl)
  50. QCoreApplication::exit(-1);
  51. }, Qt::QueuedConnection);
  52. engine.load(url);
  53.  
  54. return app.exec();
  55. }
To copy to clipboard, switch view to plain text mode 

The model is implemented as following:

sqlcredentialsmodel.h


Qt Code:
  1. #ifndef SQLCREDENTIALSMODEL_H
  2. #define SQLCREDENTIALSMODEL_H
  3.  
  4. #include <QSqlQueryModel>
  5.  
  6. class SqlCredentialsModel : public QSqlQueryModel
  7. {
  8. public:
  9. SqlCredentialsModel(QObject *parent = 0);
  10. };
  11.  
  12. #endif // SQLCREDENTIALSMODEL_H
To copy to clipboard, switch view to plain text mode 


sqlcredentialsmodel.cpp


Qt Code:
  1. #include "sqlcredentialsmodel.h"
  2.  
  3. #include <QDebug>
  4. #include <QSqlError>
  5. #include <QSqlQuery>
  6.  
  7. static void createTable()
  8. {
  9. for (auto table: QSqlDatabase::database().tables()){
  10. qDebug()<<"table "<<table;
  11. }
  12.  
  13. if (QSqlDatabase::database().tables().contains(QStringLiteral("Credentials"))) {
  14. // The table already exists; we don't need to do anything.
  15. return;
  16. }
  17.  
  18. QSqlQuery query;
  19. if (!query.exec(
  20. "CREATE TABLE IF NOT EXISTS 'Credentials' ("
  21. " 'username', 'password' TEXT NOT NULL,"
  22. " PRIMARY KEY(username)"
  23. ")")) {
  24. qFatal("Failed to query database: %s", qPrintable(query.lastError().text()));
  25. }
  26.  
  27. query.exec("INSERT INTO Credentials VALUES('bert', 'b&c' )");
  28. query.exec("INSERT INTO Credentials VALUES('emmy', 'e&c' )");
  29. query.exec("INSERT INTO Credentials VALUES('hans', 'h&c' )");
  30. }
  31.  
  32. SqlCredentialsModel::SqlCredentialsModel(QObject *parent) :
  33. {
  34. createTable();
  35.  
  36. QSqlQuery query;
  37.  
  38. if (!query.exec("SELECT * FROM Credentials"))
  39. qFatal("Contacts SELECT query failed: %s", qPrintable(query.lastError().text()));
  40.  
  41. setQuery(query);
  42. if (lastError().isValid())
  43. qFatal("Cannot set query on SqlCredentialsModel: %s", qPrintable(lastError().text()));
  44. }
To copy to clipboard, switch view to plain text mode 

QML is implemented as following:

main.qml


Qt Code:
  1. import QtQuick 2.12
  2. import QtQuick.Controls 2.5
  3.  
  4. ApplicationWindow {
  5. visible: true
  6. width: 100
  7. height: 120
  8. title: qsTr("Credentials")
  9.  
  10. SwipeView {
  11. id: swipeView
  12. anchors.fill: parent
  13.  
  14. CredentialsView{
  15. id: credentialsView
  16. }
  17. }
  18. }
To copy to clipboard, switch view to plain text mode 


CredentialsView.ui.qml


Qt Code:
  1. import QtQuick 2.9
  2. import QtQuick.Controls 2.2
  3. import QtQuick.Layouts 1.3
  4.  
  5. import credentials 1.0
  6.  
  7. Page {
  8. id: page_Credentials
  9. header: Label {
  10. text: qsTr("Credentials")
  11. font.pixelSize: Qt.application.font.pixelSize
  12. padding: 10
  13. }
  14.  
  15. CredentialsTableView{
  16. anchors.fill: parent
  17. }
  18.  
  19. }
To copy to clipboard, switch view to plain text mode 

CredentialsTableView.ui.qml

Qt Code:
  1. import QtQuick 2.12
  2. import QtQuick.Controls 2.12
  3.  
  4. import credentials 1.0
  5.  
  6. TableView {
  7. id: credentialsTableView
  8. property alias credentialsTableView: credentialsTableView
  9. anchors.fill: parent
  10. columnSpacing: 2
  11. rowSpacing: 2
  12. clip: true
  13.  
  14. model: SqlCredentialsModel{}
  15. delegate: Rectangle {
  16. color: "lightblue"
  17. implicitWidth: lbl_tableCell.width
  18. implicitHeight: lbl_tableCell.height
  19. Label {
  20. id: lbl_tableCell
  21. text: display
  22. }
  23. }
  24. }
To copy to clipboard, switch view to plain text mode 

Thanks in advance