Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Legare cambio immagine qml a file logica cpp

    Italian
    2
    2
    181
    Loading More Posts
    • 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.
    • Montanaro
      Montanaro last edited by Montanaro

      Ho disegnato due quadrati in qml :

      import QtQuick 2.9
      import QtQuick.Window 2.2
      
      Window {
          visible: true
          width: 640
          height: 480
          title: qsTr("Hello World")
      
      
          Rectangle {
              width: 100
              height: 100
              color: "red"
              border.color: "black"
              border.width: 5
              radius: 10
          }
      
      
          Rectangle {
      
              x: 100
              width: 100
              height: 100
              color: "blue"
              border.color: "black"
              border.width: 5
              radius: 10
          }
      }
      

      Ho costruito un file logica.h e logica.cpp
      in cui voglio far girare un contatore ogni secondo e stampare il progressivo del conteggio e per ciascuno secondo alternare il quadrato rosso e blu.

      logica.h :

      #ifndef LOGICA_H
      #define LOGICA_H
      
      #include <QObject>
      #include <QTcpServer>
      #include <QTcpSocket>
      #include <QThread>
      
      class Logica : public QObject
      {
          Q_OBJECT
      
      public:
          explicit Logica(QObject *parent = 0);
           static void sleep(unsigned long  msecs) {QThread:: sleep(msecs);}
      
      private:
      
      private slots:
      void StateMachine();
      signals:
      };
      #endif //LOGICA_H
      

      logica.cpp:

      #include <QDebug>
      
      int static count=0;
      
      Logica::Logica(QObject *parent) :
          QObject(parent)
      {
         QTimer *timer = new QTimer(this);
         timer->start(1000);             // timer su 1 secondo
         connect(timer, SIGNAL(timeout()), this, SLOT(StateMachine()));
      }
      
      void Logica:: StateMachine()
      {
          //  sleep(1);
          count++;
          qDebug()<< " valore del contatore è: " << count;
          if (count==100)
              count=0;
      }
      

      Non riesco a capire
      come legare la visualizzazione dei rettangoli disegnati nel qml con l'avanzare progressivo della variabile count inclusa nel file .cpp

      ODБOï 1 Reply Last reply Reply Quote 0
      • ODБOï
        ODБOï @Montanaro last edited by

        @Montanaro Ciao

        svariati modi
        https://doc.qt.io/qt-5/qtqml-cppintegration-overview.html

        Exposing Attributes of C++ Types to QML
        https://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html

        1 Reply Last reply Reply Quote 0
        • First post
          Last post