Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

qmlRegisterType sqlquerylmodel (subclass) not show on tableView



  • I use these sample for connect my sqlite db with tableview in qml quick 2 .... my ui.qml file import table view in these way:

    import QtQuick 2.3
    import QtQuick.Controls 2.3
    import QtQuick.Controls 1.4 as C
    import QtDataVisualization 1.3
    import QtQuick.LocalStorage 2.0
    import QtQuick.Window 2.10
    
    /* some code */
    
                   C.TableView{
                               id: tableViewXX
    
    
    

    for create sqlmodeldata I subclass it as explained in these wiki (last example) ... so I would to create a qmlregisterType for use it in some ui.qml page with tableView .... is right these or one time that my newSqlModelData1 and newSqlModelData2 I can use it witouth register and import it?

    regards



  • @gfxx actually I insert my

    model: SqlQueryModel
    

    in my page and print the table in these way :

    import QtQuick 2.3
    import QtQuick.Controls 2.3
    import QtQuick.Controls 1.4 as C
    import QtDataVisualization 1.3
    import QtQuick.LocalStorage 2.0
    import QtQuick.Window 2.10
    import QtSensors 5.9
    import Qt.labs.calendar 1.0
    import QtGraphicalEffects 1.0
    import QtQuick.Controls.Imagine 2.3
    import QtQuick.Extras 1.4
    import QtQuick.XmlListModel 2.0
    import QtQml.Models 2.1
    import QtCharts 2.0
    import QtQuick.Layouts 1.1
    import userRegistry 1.0
    
    /* some code about layout */
    
    
                       C.TableView {
                            id: tableViewUser
                            x: 8
                            y: 8
                            width: 1152
                            height: 675
                            verticalScrollBarPolicy: 2
                            anchors.fill: parent
                            clip: true
    
                            C.TableViewColumn {
                                title: "User id"
                                role: "IDuser"
                            }
                            C.TableViewColumn {
                                title: "Nome"
                                role: "nome"
                            }
                            C.TableViewColumn {
                                title: "Cognome"
                                role: "Cognome"
                            }
                            C.TableViewColumn {
                                title: "Data di Nascita"
                                role: "nascita"
                            }
                            C.TableViewColumn {
                                title: "Comune di Provenienza"
                                role: "localita"
                            }
    
                            model: SqlQueryModel
    
    
                        }
                    }
    

    where my query if "SELECT * FROM userData" where IDuser, nome, Cognome, nascita and localita is the name of DB table column .... i try to make these query for debug all works ok ... but when I use model in qml file the TableView show empty row and cell ....

    in my main.cpp i use sqlquerymodel subclassed in these way:

        SqlQueryModel *userData = new SqlQueryModel(nullptr);
        userData->setQuery("SELECT * FROM userData");
    
        qmlRegisterType<SqlQueryModel>("userRegistry", 1, 0, "SqlQueryModel");
    

    someone can help me about these ??

    regards



  • I answer myself:

    • basically I not know qml quick world ... but the solution is not abstractmodel but the right setting of context.

    so my new main code (working) is these:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QtQuickControls2>
    #include <QtSql>
    #include <QtGui/QGuiApplication>
    #include <QtCore/QDir>
    #include <QtQuick/QQuickView>
    #include <QtQml/QQmlEngine>
    #include <QtQml/qqml.h>
    #include "shared/shared.h"
    #include <QtCharts>
    #include <QtDataVisualization>
    #include <QAbstractTableModel>
    #include <QtMessageHandler>
    #include <QMessageBox>
    #include <QMessageLogContext>
    #include <QMessageLogger>
    #include <QDebug>
    #include <QQmlEngine>
    #include "sqlquerymodel.h"
    
    
    using namespace QtCharts;
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
       QGuiApplication app(argc, argv);
    
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName("/home/pc/Qt/TestApp/DataBase/TestAnalyticsDB");
        if(db.open()) {//{ QMessageBox::information(nullptr,"Ok","Connection ok");
            qDebug() << "connessione ok";
        }
        else qDebug() << "connessione FALLITA";//QMessageBox::information(nullptr,"Error","some error.");
    
    
        SqlQueryModel *UserRegistry = new SqlQueryModel(nullptr);
        UserRegistry->setQuery("SELECT * FROM userData");
        SqlQueryModel *AdmRegistry = new SqlQueryModel(nullptr);
        AdmRegistry->setQuery("SELECT * FROM admData");
    
    
        QQmlApplicationEngine engine;
        engine.rootContext()->setContextProperty("userModel", UserRegistry);  /* the trick */
        engine.rootContext()->setContextProperty("admModel", AdmRegistry);  /* second query table model */
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
    
        return app.exec();
    }
    

    after these in ui.qml part in tableView section is simple to write these :

    import QtQuick 2.3
    import QtQuick.Controls 2.3
    import QtQuick.Controls 1.4 as C
    
    /*some code*/
     C.TableView {
                            id: tableViewUser
                            x: 8
                            y: 8
                            width: 1152
                            height: 244
                            anchors.right: parent.right
                            anchors.left: parent.left
                            anchors.top: parent.top
                            verticalScrollBarPolicy: 2
                            clip: true
    
                            model: userModel     /* the trick */
    
                            C.TableViewColumn {
                                title: "User id"
                                role: "IDuser"
                                width: title.length * 10
                                visible: true
                            }
    
    /* other column*/
    /* second table*/
    
                        C.TableView {
                            id: tableViewData
                            x: 0
                            y: 250
                            width: 1168
                            height: 433
                            anchors.right: parent.right
                            anchors.left: parent.left
                            anchors.top: parent.bottom
                            verticalScrollBarPolicy: 2
                            clip: true
    
                            model: admModel   /* second query table model */
    
                            C.TableViewColumn {
                                title: "Sensore 1"
                                role: "sens1"
                                width: title.length * 15
                            }
    

    with SqlQueryModel class i symple to show every type of query in a table (in these case in quick 1.4 near to 2.3 control, but I think is the same with new TableView in Qt5.12).

    regards


Log in to reply