QML interacting with C + +: Using Qsqlquerymodel to display database data in QML

Source: Internet
Author: User
Tags wrapper

QML interacting with C + +: Using Qsqlquerymodel to display database data in QML


This blog link: http://blog.csdn.net/jdh99, author: jdh, reprint please specify.


Reference Links:

Http://qt-project.org/wiki/How_to_use_a_QSqlQueryModel_in_QML


Environment:

Host: WIN7

Development environment: Qt5.2.1


Description

The database cannot be manipulated directly in QML, so the Qsqlquerymodel is encapsulated as a subclass and used as a property for QML


:



Source:


The code in the QML file that is responsible for data hosting display:

< Span style= "line-height:25.98958396911621px" >

Component {id:msndelegate Item {id:wrapper width : Grid.cellwidth; Height:grid.cellHeight Column {image{Source: "Pics/light_on.png"; ancho Rs.horizontalCenter:parent.horizontalCenter; Width:grid.cellWidth * 0.7; Height:grid.cellHeight * 0.7} Text {text:ctrl_id;anchors.horizontalcenter:parent.horizontalcenter; Color:wrapper. Gridview.iscurrentitem? "Red": "Blue"} Text {text:name;anchors.horizontalcenter:parent.horizontalcenter; color:wrapper. Gridview.iscurrentitem?                    "Red": "Blue"}} mousearea {anchors.fill:parent OnClicked:grid.currentIndex = Index}}} GridView {Id:gr ID//anchors.fill:parent width:parent.width Height:parent.height-space1.heiGht anchors {top:space1.bottom;} CellWidth:parent.width * 0.25 CellHeight:parent.width * 0.25//model:listmodel Model:            Myfirstmodel delegate:msndelegate highlight:rectangle {color: "Lightsteelblue"; Radius:5} Currentindex:2//focus:true}

C + + code:

Sqlquerymodel.h

#ifndef sqlquerymodel_h#define sqlquerymodel_h#include <qsqlquerymodel>class sqlquerymodel:public qsqlquerymodel{    q_object    void Generaterolenames ();p ublic:    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;    Virtual Qhash<int, qbytearray> rolenames () const;signals:public slots:}; #endif//Sqlquerymodel_h

Sqlquerymodel.cpp

< Span style= "line-height:25.98958396911621px" >

#include "sqlquerymodel.h" #include <QSqlRecord> #include <QSqlField> #include <QDebug> Sqlquerymodel::sqlquerymodel (Qobject *parent): Qsqlquerymodel (parent) {}void sqlquerymodel::setquery (const QString &    Amp;query, const qsqldatabase &db) {qsqlquerymodel::setquery (query,db); Generaterolenames ();}    void Sqlquerymodel::setquery (const qsqlquery & query) {qsqlquerymodel::setquery (query); Generaterolenames ();}    void Sqlquerymodel::generaterolenames () {qhash<int, qbytearray> rolenames;    for (int i = 0; I < record (). Count (); i++) {rolenames[qt::userrole + i + 1] = record (). FieldName (i). ToUtf8 (); }//setrolenames (rolenames);}    Qhash<int, qbytearray> sqlquerymodel::rolenames () const{qhash<int, qbytearray> roleNames;    for (int i = 0; I < record (). Count (); i++) {rolenames[qt::userrole + i + 1] = record (). FieldName (i). ToUtf8 (); } return rolenames;} Qvariant Sqlquerymodel::d ata (const qmodelindex &index, int role) const{qvariant value = Qsqlquerymodel::d ata (index, role);    if (role < qt::userrole) {value = Qsqlquerymodel::d ata (index, role);        } else {int columnidx = role-qt::userrole-1;        Qmodelindex Modelindex = This->index (Index.row (), COLUMNIDX);    Value = Qsqlquerymodel::d ata (Modelindex, Qt::D isplayrole); } return value;

In the main function code associated with the data:
    Sqlquerymodel *model1 = new Sqlquerymodel (0);    Model1->setquery ("SELECT * from Ctrl_para");    Qtquick2applicationviewer Viewer;    Viewer.rootcontext ()->setcontextproperty ("Myfirstmodel", model1);    Viewer.setmainqmlfile (Qstringliteral ("qml/sh_user/base.qml"));    Viewer.showexpanded ();

Attention:

The query operation executes only once, so if you want to display the data in the database in real time, you can read the database periodically to display the data dynamically.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.