Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Do not show data in table mysql In Tableview
Forum Updated to NodeBB v4.3 + New Features

Do not show data in table mysql In Tableview

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmlsqlquerymodel
4 Posts 3 Posters 1.3k Views 2 Watching
  • 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.
  • MrErfanM Offline
    MrErfanM Offline
    MrErfan
    wrote on last edited by
    #1

    hi guys ,
    I want to give a series of data in the table (MYSQL) in QML View ،
    To do this I created a class sqlquerymodel ، I use the following code to display
    When I open the program, there is no data in the table, what's the problem?

    //h
    #include <QSqlQueryModel>
    
    class SqlQueryModel : public QSqlQueryModel
    {
        Q_OBJECT
    
    public:
        explicit SqlQueryModel(QObject *parent = 0);
    
        void setQuery(const QString &query, const QSqlDatabase &db = QSqlDatabase());
        void setQuery(const QSqlQuery &query);
        QVariant data(const QModelIndex &index, int role) const;
        QHash<int, QByteArray> roleNames() const {	return m_roleNames;	}
    
    private:
        void generateRoleNames();
        QHash<int, QByteArray> m_roleNames;
    };
    
    //cpp
    #include "SqlQueryModel.h"
    #include <QSqlRecord>
    #include <QSqlField>
    
    SqlQueryModel::SqlQueryModel(QObject *parent) :
        QSqlQueryModel(parent)
    {
    }
    
    void SqlQueryModel::setQuery(const QString &query, const QSqlDatabase &db)
    {
        QSqlQueryModel::setQuery(query, db);
        generateRoleNames();
    }
    
    void SqlQueryModel::setQuery(const QSqlQuery & query)
    {
        QSqlQueryModel::setQuery(query);
        generateRoleNames();
    }
    
    void SqlQueryModel::generateRoleNames()
    {
        m_roleNames.clear();
        for( int i = 0; i < record().count(); i ++) {
            m_roleNames.insert(Qt::UserRole + i + 1, record().fieldName(i).toUtf8());
        }
    }
    
    QVariant SqlQueryModel::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;
    }
    
    
    //main.cpp
        SqlQueryModel *sqlquerymodel = new SqlQueryModel();
        sqlquerymodel->setQuery("SELECT * FROM tbl_user");
        engine.rootContext()->setContextProperty("SqlQueryModel", sqlquerymodel);
    
    //main.qml
        TableView {
            id: tableview
            anchors.fill: parent
            anchors.margins: 10
            model: SqlQueryModel
            TableViewColumn { role: "Id" ; title: "Id"; visible: true }
            TableViewColumn { role: "UserName" ; title: "UserName" }
    
        }
    
    1 Reply Last reply
    0
    • MrErfanM Offline
      MrErfanM Offline
      MrErfan
      wrote on last edited by
      #2

      Not somebody help me? :(

      p3c0P 1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Please practice some patience, allow 24 hours to run before bumping your own thread. This forum is community driven and not all people live in the same timezone as you.

        That said, did you check that your database connection is properly opened before running your queries ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • MrErfanM MrErfan

          Not somebody help me? :(

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          @MrErfan Are you sure the role's assigned in QML match with those returned from the model ? They are case-sensitive.

          157

          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