Unsolved Accedere a Qlist da QML
-
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!!
-
@Sierra fallo in c++ e poi passa i risultati a qml ....
-
This post is deleted! -
This post is deleted! -
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