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

Printout data from mysql table using QSqlQueryModel



  • I am using the following example https://wiki.qt.io/How_to_Use_a_QSqlQueryModel_in_QML. It uses QSqlQueryModel.
    But in my case, I do not know the table column names in the output as I am using * query "Select * from users". How do I print the data in TableView in Qml.

    My code is below:

    QtTest2.cpp

    QtTest2::QtTest2(QObject *parent) :
        QSqlQueryModel(parent)
    {
    }
    
    void QtTest2::setQuery(const QString &query, const QSqlDatabase &db)
    {
        QSqlQueryModel::setQuery(query, db);
        generateRoleNames();
    }
    
    void QtTest2::setQuery(const QSqlQuery & query)
    {
        QSqlQueryModel::setQuery(query);
        generateRoleNames();
    }
    
    void QtTest2::generateRoleNames()
    {
        m_roleNames.clear();
        for( int i = 0; i < record().count(); i ++) {
            m_roleNames.insert(Qt::UserRole + i + 1, record().fieldName(i).toUtf8());
        }
    }
    
    QVariant QtTest2::data(const QModelIndex &index, int role) const
    {
        QVariant value;
    
        if(role < Qt::UserRole) {
            value = QSqlQueryModel::data(index, role);
        }
        else {
            int columnIdx = role - Qt::UserRole - 1;
            QModelIndex modelIndex = this->index(index.row(), columnIdx);
            value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
        }
        return value;
    }
    
    void QtTest2::callSql()
    {
        this->setQuery("SELECT * FROM users");
    }
    

    Qml file

    TableView {
        id: tableView
        model: QtTest2
    
        // This is for header
    
        Row {
            id: columnsHeader
            y: tableView.contentY
            z: 2
            Repeater {
                model: tableView.columns > 0 ? tableView.columns : 1
                Label {
                    width: tableView.columnWidthProvider(modelData)
                    height: 35
                    text: QtTest2.headerData(modelData, Qt.Horizontal)
                    color: '#aaaaaa'
                    font.pixelSize: 15
                    padding: 10
                    verticalAlignment: Text.AlignVCenter
    
                    background: Rectangle { color: "#333333" }
                }
            }
        }
    
    // Here I cannot print the values dynamically. 
    // Currently this is printing the email (e.g.), in all columns in a row
    // But yes, each row prints different email
    // I want the rest of the column data dynamically printed
      
    delegate:Rectangle {
                        Text {
                            text: email
                            anchors.fill: parent
                            anchors.margins: 10
                            color: 'black'
                            font.pixelSize: 15
                            verticalAlignment: Text.AlignVCenter
                        }
                    }
       
    


  • It was resolved using the following code in QML

    text: modelData // display

    delegate:Rectangle {
                        Text {
                            text: modelData // display
                            anchors.fill: parent
                            anchors.margins: 10
                            color: 'black'
                            font.pixelSize: 15
                            verticalAlignment: Text.AlignVCenter
                        }
                    }
    


  • It was resolved using the following code in QML

    text: modelData // display

    delegate:Rectangle {
                        Text {
                            text: modelData // display
                            anchors.fill: parent
                            anchors.margins: 10
                            color: 'black'
                            font.pixelSize: 15
                            verticalAlignment: Text.AlignVCenter
                        }
                    }
    

Log in to reply