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.4k 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.
  • M Offline
    M Offline
    MrErfan
    wrote on 1 Feb 2016, 11:48 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
    • M Offline
      M Offline
      MrErfan
      wrote on 1 Feb 2016, 17:34 last edited by
      #2

      Not somebody help me? :(

      P 1 Reply Last reply 2 Feb 2016, 11:28
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 1 Feb 2016, 21:54 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
        • M MrErfan
          1 Feb 2016, 17:34

          Not somebody help me? :(

          P Offline
          P Offline
          p3c0
          Moderators
          wrote on 2 Feb 2016, 11:28 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

          4/4

          2 Feb 2016, 11:28

          • Login

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