Impostare Qml da c++ - set QML from C++
-
Buonasera a tutti, sono nuovo di qml, in pratica vorrei stampare su una finestra il risultato di una query all'interno dei textInput, il db e la query sono ok..ma ora non so come assegnare i valori che ottengo al textInput ....
Vi ringrazio
FrancescoGood evening everyone, I'm new in QML, in practice I would like to print on a window, the result of a query within the textInput, db and query are ok..but now do not know how to assign the values that I get to textInput .. ..
Thank you
Francesco -
Ho visto http://stackoverflow.com/questions/27584803/how-to-set-qml-properties-from-c
ma non riesco.... Nessuno sà aiutarmi?I see http://stackoverflow.com/questions/27584803/how-to-set-qml-properties-from-c but not work.... Can you help me?
-
Ciao,
il modo più rapido è di passare dei valori al context qml da C++.Qui è documentato piuttosto bene:
http://doc.qt.io/qt-5/qtqml-cppintegration-contextproperties.html -
Scusami è un pò troppo complesso per mè, ma proverò....
questo è il mio codice://main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlTableModel>
#include <QDebug>
#include <QObject>int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName("localhost"); db.setDatabaseName("Nome_db"); db.setUserName("root"); db.setPassword("XXXXXXXX"); bool ok = db.open(); QSqlQuery query; query.exec("SELECT * FROM upload;"); while (query.next()) { int id = query.value(0).toInt(); QString descrizione = query.value(1).toString(); QString filename = query.value(2).toString(); int id_dipendente = query.value(3).toInt(); qDebug() << id << descrizione << filename << id_dipendente; } view_id->setProperty("width", 500); return app.exec();
}
8//MainForm.ui.qml
import QtQuick 2.6
Rectangle {
width: 360 height: 360 color: "gray" TextEdit { id: id x: 8 y: 30 width: 300 height: 20 font.pixelSize: 12 objectName: "view_id" } TextEdit { id: descrizione x: 8 y: 60 width: 300 height: 20 font.pixelSize: 12 } TextEdit { id: filename x: 8 y: 90 width: 300 height: 20 font.pixelSize: 12 } TextEdit { id: id_dipendente x: 8 y: 120 width: 300 height: 20 font.pixelSize: 12 }
Vorrei mettere i dati ottenuti in sql dentro i textarea..
Proverò a vedere quel che mi hai suggerito...Grazie 1000
} -
Grazie a synasius ho capito....
bisogna includere nel cpp:
<< #include <QQmlContext> >>
ed aggiungere questa stringa:
<< auto* ctx = engine.rootContext(); >>
e aggiungere per ogni textinput
<< ctx->setContextProperty("view_id",VALORE); >>
dove view_id è un textedit:
<<
TextEdit {
id: id
x: 8
y: 30
width: 300
height: 20
font.pixelSize: 12
text: view_id
}Così il mio codice è diventato:
<< //main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlTableModel>
#include <QDebug>
#include <QObject>
#include <QQmlContext>int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName("localhost"); db.setDatabaseName("Nome_Db"); db.setUserName("root"); db.setPassword("mysql"); bool ok = db.open(); auto* ctx = engine.rootContext(); QSqlQuery query; query.exec("SELECT * FROM upload;"); while (query.next()) { ctx->setContextProperty("view_id", query.value(0).toInt()); ctx->setContextProperty("view_desc", query.value(1).toString()); ctx->setContextProperty("view_filename", query.value(2).toString()); ctx->setContextProperty("view_id_dip", query.value(3).toInt()); //qDebug() << id << descrizione << filename << id_dipendente; } return app.exec();
}
<< //MainForm.ui.qml
import QtQuick 2.6Rectangle {
width: 360 height: 360 color: "gray" TextEdit { id: id x: 8 y: 30 width: 300 height: 20 font.pixelSize: 12 text: view_id } TextEdit { id: descrizione x: 8 y: 60 width: 300 height: 20 font.pixelSize: 12 text: view_desc } TextEdit { id: filename x: 8 y: 90 width: 300 height: 20 font.pixelSize: 12 text: view_filename } TextEdit { id: id_dipendente x: 8 y: 120 width: 300 height: 20 text: view_id_dip font.pixelSize: 12 }
}
Grazie ;)