Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Italian
  4. Impostare Qml da c++ - set QML from C++
Forum Update on Monday, May 27th 2025

Impostare Qml da c++ - set QML from C++

Scheduled Pinned Locked Moved Solved Italian
5 Posts 2 Posters 1.8k 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.
  • darkstaringD Offline
    darkstaringD Offline
    darkstaring
    wrote on last edited by darkstaring
    #1

    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
    Francesco

    Good 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

    1 Reply Last reply
    0
    • darkstaringD Offline
      darkstaringD Offline
      darkstaring
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        synasius
        wrote on last edited by synasius
        #3

        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

        1 Reply Last reply
        1
        • darkstaringD Offline
          darkstaringD Offline
          darkstaring
          wrote on last edited by darkstaring
          #4

          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
          }

          1 Reply Last reply
          0
          • darkstaringD Offline
            darkstaringD Offline
            darkstaring
            wrote on last edited by darkstaring
            #5

            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.6

            Rectangle {

            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 ;)

            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