Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Printout data from mysql table using QSqlQueryModel
Qt 6.11 is out! See what's new in the release blog

Printout data from mysql table using QSqlQueryModel

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 306 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chilarai
    wrote on last edited by chilarai
    #1

    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
                        }
                    }
       
    
    1 Reply Last reply
    0
    • C Offline
      C Offline
      chilarai
      wrote on last edited by
      #2

      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
                          }
                      }
      
      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved