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. Cannot display qsqlquerymodel to a qml list view

Cannot display qsqlquerymodel to a qml list view

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 2.1k 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
    chatzich
    wrote on last edited by
    #1

    @ int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);

    QFile f("productList.sqlite");
     
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setHostName("localhost");
    db.setDatabaseName("productdb");
    db.setUserName("user");
    db.open();
    QTextStream in(&f);
    QString line;
    if (f.open(QFile::ReadOnly | QFile::Text)) {
    QTextStream in(&f);
    line = in.readLine();
    while(!line.isNull()) {
    QSqlQuery query = db.exec(line);
    qDebug() << line;
    line = in.readLine();
    }
    }
     
    f.close();
     
    QSqlQuery query1 = db.exec&#40;"SELECT * FROM productList"&#41;;
    qDebug() << query1.size();
    QSqlQueryModel *model = new QSqlQueryModel;
     
    model->setQuery("SELECT * FROM productList", db);
    qDebug() << model->record(0);
    model->setHeaderData(0, Qt::Horizontal, QObject::tr("Categorylv1"));
    model->setHeaderData(1, Qt::Horizontal, QObject::tr("Categorylv2"));
     
    model->setHeaderData(2, Qt::Horizontal, QObject::tr("Categorylv3"));
    model->setHeaderData(3, Qt::Horizontal, QObject::tr("productCode"));
    model->setHeaderData(4, Qt::Horizontal, QObject::tr("productDescription"));
    model->setHeaderData(5, Qt::Horizontal, QObject::tr("productPrice"));
     
    QtQuick2ApplicationViewer viewer;
    viewer.rootContext()->setContextProperty("myModel", model);
    viewer.setMainQmlFile&#40;QStringLiteral("qml/basket/main.qml"&#41;);
    viewer.showExpanded();
     
    return app.exec();
    }@
    

    @ import QtQuick 2.0

    Rectangle {
    width: 360
    height: 360
    // color: "black"
     
    Grid {
    id: grid1
    anchors.rightMargin: 0
    anchors.bottomMargin: 0
    anchors.leftMargin: 0
    anchors.topMargin: 0
    anchors.fill: parent
     
    Text {
    id: text1
    text: qsTr("Text")
    anchors.right: parent.right
    anchors.left: parent.left
    anchors.top: parent.top
    font.pixelSize: 12
    }
     
    ListView {
    id: listView1
    model:myModel
    x: 0
    y: 0
    anchors.topMargin: 27
    anchors.fill: parent
    delegate: Item {
     
    Row {
    anchors.margins: 4
    anchors.fill: parent
    spacing: 4;
     
    Text {
    text: productDescription
    width: 150
    }
    }
    }
    }
    }
    }
    

    @

    Hello everyone I try some code with qml and i want to see to a listview some items with productsDescription I tried the example i found to web but the result is an empty list any ideas what I do wrong?

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      I think there are multiple issues here.
      Please have a look at following examples:
      "integrating-sqlite-with-qt-quick":http://stackoverflow.com/questions/16658360/integrating-sqlite-with-qt-quick
      "How_to_use_a_QSqlQueryModel_in_QML":http://qt-project.org/wiki/How_to_use_a_QSqlQueryModel_in_QML
      "qtquickcontrols-calendar-example":https://qt-project.org/doc/qt-5/qtquickcontrols-calendar-example.html

      The best way would be to subclass QSqlQueryModel as explained in the above examples.

      157

      1 Reply Last reply
      0
      • C Offline
        C Offline
        chatzich
        wrote on last edited by
        #3

        Consider problem SOLVED i solved it with implementing @class SqlQueryModel: public QSqlQueryModel
        {
        Q_OBJECT
        QHash<int,QByteArray> *hash;
        public:
        explicit SqlQueryModel(QObject * parent) : QSqlQueryModel(parent)
        {
        hash = new QHash<int,QByteArray>;
        hash->insert(Qt::UserRole, QByteArray("someRoleName"));
        hash->insert(Qt::UserRole + 1, QByteArray("otherRoleName"));
        }
        QVariant data(const QModelIndex &index, int role) const
        {
        if(role < Qt::UserRole) {
        return QSqlQueryModel::data(index, role);
        }
        QSqlRecord r = record(index.row());
        return r.value(QString(hash->value(role))).toString();
        }
        inline RoleNameHash roleNames() const { return *hash; }
        };@

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          Wonderful :)
          You can mark post as solved by editing this post title and prepend [solved] so that others know it has been solved.

          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