Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Italian
  4. Accedere a Qlist da QML
Forum Update on Monday, May 27th 2025

Accedere a Qlist da QML

Scheduled Pinned Locked Moved Unsolved Italian
5 Posts 5 Posters 754 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.
  • S Offline
    S Offline
    Sierra
    wrote on last edited by
    #1

    Ciao a tutti,

    sono nuovo del forum e abbastanza nuovo anche de mondo QT, sono alle prese con una semplice applicazione e mi sto scontrando con con un problema di "comunicazione" tra QML e C++, provo a descriverlo, ringrazio tutti per ogni forma di aiuto.

    Ho una classe MyUDP, che nel costruttore riempie una lista ed emette un segnale:

    MyUDP::MyUDP(QObject *parent) : QObject(parent)
    {
        qDebug() << "MyUDP Constructor";
        m_EthInterfList.clear();
        /* List all interfaces */
        foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces() )
        {
            if (interface.flags().testFlag(QNetworkInterface::IsUp) && !interface.flags().testFlag(QNetworkInterface::IsLoopBack))
            {
                foreach (QNetworkAddressEntry entry, interface.addressEntries())
                {
                    if ( interface.hardwareAddress() != "00:00:00:00:00:00" && entry.ip().toString().contains("."))
                    {
                        NetworkInterfaces* tmpEthIf = new NetworkInterfaces(    interface.name(),
                                                                            entry.ip().toString().trimmed(),
                                                                            entry.netmask().toString().trimmed(),
                                                                            entry.broadcast().toString().trimmed() );
                        m_EthInterfList.append(tmpEthIf);
                        emit ethlistChanged();
                    }
                }
            }
        }
    }
    

    il file myudp.h è così composto:

    #ifndef MYUDP_H
    #define MYUDP_H
    
    #include <QObject>
    #include <QUdpSocket>
    #include "networkinterfaces.h"
    
    class MyUDP : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QList<NetworkInterfaces*> ethlist     READ getEthList                       NOTIFY ethlistChanged)
    
    public:
        explicit MyUDP(QObject *parent = 0);
        QList<NetworkInterfaces*> getEthList () const { return m_EthInterfList; }
    
    signals:
        void ethlistChanged();
    
    public slots:
        void readyRead();
        void sayHello();
        void printInterfaces();
    
    
    private:
        QUdpSocket *socket;
        QList <NetworkInterfaces*> m_EthInterfList;
        QByteArray m_type;
    };
    
    #endif // MYUDP_H
    

    Nel file main.qml go inserito questa entry:

    import QtQuick 2.11
    import QtQuick.Window 2.11
    import QtQuick.Layouts 1.3
    import QtQuick.Controls 1.4
    import MyUDP 1.0
    Window {
    
        MyUDP {
            id: myudp
            onEthlistChanged: :{
                console.log("Ethlist Changed!!!" );
    
            }
        }
    
    

    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    
    #include "myudp.h"
    
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
        qmlRegisterType<MyUDP>("MyUDP", 1, 0, "MyUDP");
    
        QQmlApplicationEngine engine;
        
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        return app.exec();
    }
    
    

    Qualsiasi prova fatta, qualsiasi lettura di documentazione/forum non mi ha sbloccato dal mio problema, non riesco a fare il catch del segnale onEthlistChanged da QML. Avete qualche suggerimento in merito?

    Grazie mille a tutti!!

    gfxxG T 2 Replies Last reply
    0
    • S Sierra

      Ciao a tutti,

      sono nuovo del forum e abbastanza nuovo anche de mondo QT, sono alle prese con una semplice applicazione e mi sto scontrando con con un problema di "comunicazione" tra QML e C++, provo a descriverlo, ringrazio tutti per ogni forma di aiuto.

      Ho una classe MyUDP, che nel costruttore riempie una lista ed emette un segnale:

      MyUDP::MyUDP(QObject *parent) : QObject(parent)
      {
          qDebug() << "MyUDP Constructor";
          m_EthInterfList.clear();
          /* List all interfaces */
          foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces() )
          {
              if (interface.flags().testFlag(QNetworkInterface::IsUp) && !interface.flags().testFlag(QNetworkInterface::IsLoopBack))
              {
                  foreach (QNetworkAddressEntry entry, interface.addressEntries())
                  {
                      if ( interface.hardwareAddress() != "00:00:00:00:00:00" && entry.ip().toString().contains("."))
                      {
                          NetworkInterfaces* tmpEthIf = new NetworkInterfaces(    interface.name(),
                                                                              entry.ip().toString().trimmed(),
                                                                              entry.netmask().toString().trimmed(),
                                                                              entry.broadcast().toString().trimmed() );
                          m_EthInterfList.append(tmpEthIf);
                          emit ethlistChanged();
                      }
                  }
              }
          }
      }
      

      il file myudp.h è così composto:

      #ifndef MYUDP_H
      #define MYUDP_H
      
      #include <QObject>
      #include <QUdpSocket>
      #include "networkinterfaces.h"
      
      class MyUDP : public QObject
      {
          Q_OBJECT
          Q_PROPERTY(QList<NetworkInterfaces*> ethlist     READ getEthList                       NOTIFY ethlistChanged)
      
      public:
          explicit MyUDP(QObject *parent = 0);
          QList<NetworkInterfaces*> getEthList () const { return m_EthInterfList; }
      
      signals:
          void ethlistChanged();
      
      public slots:
          void readyRead();
          void sayHello();
          void printInterfaces();
      
      
      private:
          QUdpSocket *socket;
          QList <NetworkInterfaces*> m_EthInterfList;
          QByteArray m_type;
      };
      
      #endif // MYUDP_H
      

      Nel file main.qml go inserito questa entry:

      import QtQuick 2.11
      import QtQuick.Window 2.11
      import QtQuick.Layouts 1.3
      import QtQuick.Controls 1.4
      import MyUDP 1.0
      Window {
      
          MyUDP {
              id: myudp
              onEthlistChanged: :{
                  console.log("Ethlist Changed!!!" );
      
              }
          }
      
      

      main.cpp

      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      
      #include "myudp.h"
      
      
      int main(int argc, char *argv[])
      {
          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
      
          QGuiApplication app(argc, argv);
          qmlRegisterType<MyUDP>("MyUDP", 1, 0, "MyUDP");
      
          QQmlApplicationEngine engine;
          
          engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
          if (engine.rootObjects().isEmpty())
              return -1;
      
          return app.exec();
      }
      
      

      Qualsiasi prova fatta, qualsiasi lettura di documentazione/forum non mi ha sbloccato dal mio problema, non riesco a fare il catch del segnale onEthlistChanged da QML. Avete qualche suggerimento in merito?

      Grazie mille a tutti!!

      gfxxG Offline
      gfxxG Offline
      gfxx
      wrote on last edited by
      #2

      @Sierra fallo in c++ e poi passa i risultati a qml ....

      bkt

      1 Reply Last reply
      0
      • S Sierra

        Ciao a tutti,

        sono nuovo del forum e abbastanza nuovo anche de mondo QT, sono alle prese con una semplice applicazione e mi sto scontrando con con un problema di "comunicazione" tra QML e C++, provo a descriverlo, ringrazio tutti per ogni forma di aiuto.

        Ho una classe MyUDP, che nel costruttore riempie una lista ed emette un segnale:

        MyUDP::MyUDP(QObject *parent) : QObject(parent)
        {
            qDebug() << "MyUDP Constructor";
            m_EthInterfList.clear();
            /* List all interfaces */
            foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces() )
            {
                if (interface.flags().testFlag(QNetworkInterface::IsUp) && !interface.flags().testFlag(QNetworkInterface::IsLoopBack))
                {
                    foreach (QNetworkAddressEntry entry, interface.addressEntries())
                    {
                        if ( interface.hardwareAddress() != "00:00:00:00:00:00" && entry.ip().toString().contains("."))
                        {
                            NetworkInterfaces* tmpEthIf = new NetworkInterfaces(    interface.name(),
                                                                                entry.ip().toString().trimmed(),
                                                                                entry.netmask().toString().trimmed(),
                                                                                entry.broadcast().toString().trimmed() );
                            m_EthInterfList.append(tmpEthIf);
                            emit ethlistChanged();
                        }
                    }
                }
            }
        }
        

        il file myudp.h è così composto:

        #ifndef MYUDP_H
        #define MYUDP_H
        
        #include <QObject>
        #include <QUdpSocket>
        #include "networkinterfaces.h"
        
        class MyUDP : public QObject
        {
            Q_OBJECT
            Q_PROPERTY(QList<NetworkInterfaces*> ethlist     READ getEthList                       NOTIFY ethlistChanged)
        
        public:
            explicit MyUDP(QObject *parent = 0);
            QList<NetworkInterfaces*> getEthList () const { return m_EthInterfList; }
        
        signals:
            void ethlistChanged();
        
        public slots:
            void readyRead();
            void sayHello();
            void printInterfaces();
        
        
        private:
            QUdpSocket *socket;
            QList <NetworkInterfaces*> m_EthInterfList;
            QByteArray m_type;
        };
        
        #endif // MYUDP_H
        

        Nel file main.qml go inserito questa entry:

        import QtQuick 2.11
        import QtQuick.Window 2.11
        import QtQuick.Layouts 1.3
        import QtQuick.Controls 1.4
        import MyUDP 1.0
        Window {
        
            MyUDP {
                id: myudp
                onEthlistChanged: :{
                    console.log("Ethlist Changed!!!" );
        
                }
            }
        
        

        main.cpp

        #include <QGuiApplication>
        #include <QQmlApplicationEngine>
        
        #include "myudp.h"
        
        
        int main(int argc, char *argv[])
        {
            QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        
            QGuiApplication app(argc, argv);
            qmlRegisterType<MyUDP>("MyUDP", 1, 0, "MyUDP");
        
            QQmlApplicationEngine engine;
            
            engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
            if (engine.rootObjects().isEmpty())
                return -1;
        
            return app.exec();
        }
        
        

        Qualsiasi prova fatta, qualsiasi lettura di documentazione/forum non mi ha sbloccato dal mio problema, non riesco a fare il catch del segnale onEthlistChanged da QML. Avete qualche suggerimento in merito?

        Grazie mille a tutti!!

        T Offline
        T Offline
        thomas1379
        Banned
        wrote on last edited by thomas1379
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • F Offline
          F Offline
          fridaysvisit
          Banned
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            La risposta alla tua domanda e' qui https://evileg.com/en/post/569/
            Sopsetto pero' che tu, alla fine sia iteressato a una cosa simile a quella descritta qui: https://wiki.qt.io/How_to_Use_a_Custom_Class_in_C%2B%2B_Model_and_QML_View

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            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