c++ and qml .. modifying properties need help
-
Hi need help to solve my problem. I have established a qml and cpp project - qt quick application, and want to use something like http://doc.qt.io/qt-4.8/qtbinding.html#receiving-signals under modifying properties.
I want to change value of a property:
property int angle1:
in my .qml- file
from a cpp file that receive emitted data via TCPserver. The data recieved shall be the value of the property in .qml.
hope i explained it right.
my code looks like:
main.qml in qrc:
property int angle1:
myserver.cpp
#include "myserver.h" #include <QSignalMapper> #include <QTcpServer> #include <QTcpSocket> #include <QQmlApplicationEngine> #include <QQmlComponent> MyServer::MyServer(QObject *parent) : QObject(parent) { QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); QObject *rootObject = engine.rootObjects().first(); GUI = rootObject->findChild<QObject*>("GUI"); server = new QTcpServer(this); connect(server, SIGNAL(newConnection()), this, SLOT(newConnection())); if(!server->listen(QHostAddress::LocalHost, 8001)) { qDebug() << "Server could not start."; } else { qDebug() << "Server started."; } } void MyServer::newConnection() { qDebug() << "Client connected"; QTcpSocket *socket = server->nextPendingConnection(); // Ugly hack! this->socket = socket; connect(socket, SIGNAL(readyRead()),this, SLOT(readyRead())); sendData("Welcome client!\r\n"); } void MyServer::sendData(std::string data) { const char * rawData = data.c_str(); socket->write(rawData); socket->flush(); socket->waitForBytesWritten(3000); } void MyServer::readyRead() { qDebug() << "Reading data..."; QByteArray data = socket->readAll(); qDebug() << data; emit dataRecievedFromServerApp(data); //QObject *object = view.rootObject(); //QObject *angle1 = GUI->findChild<QObject*>("angle1"); //angle1->setProperty("angle1", 160); qDebug() << "Done reading data."; sendData("Server received data, thank you!\r\n"); } void MyServer::onDisconnected() { socket->close(); }
main.cpp
#include <QApplication> #include <QQmlApplicationEngine> #include <myserver.h> int main(int argc, char *argv[]) { QApplication app(argc, argv); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); MyServer mServer; return app.exec(); }
myserver.h
#ifndef MYSERVER_H #define MYSERVER_H #include <QObject> #include <QDebug> #include <QTcpServer> #include <QTcpSocket> #include <QHostAddress> #include <QObject> #include <QSignalMapper> class MyServer : public QObject { Q_OBJECT public: explicit MyServer(QObject *parent = 0); void sendData(std::string data); signals: void dataRecievedFromServerApp(QByteArray data); public slots: void newConnection(); void readyRead(); void onDisconnected(); private: QTcpServer *server; QTcpSocket *socket; QObject *GUI; QSignalMapper *m_readyReadSignalMapper; QSignalMapper *m_disconnectedSignalMapper; }; #endif // MYSERVER_H
-
when i run the application like above it comes no errors and it run the program but I have not solve the problem to get the property in qml to be the value of "data" in
emit dataRecievedFromServerApp(data)
in myserver.cpp
but when I try with
QObject *object = view.rootObject(); QObject *angle1 = GUI->findChild<QObject*>("angle1"); angle1->setProperty("angle1", 160);
it stops work
-
@antemort First check if that QML contains no error. Secondly you need to
findChild
in therootObject
QQuickItem *rootItem = view.rootObject(); //root item in QML QQuickItem *child = parent->findChild<QQuickItem*>("childobjectname"); //find child of that root object using its objectName child->setProperty("angle1", 160); //set property angle1 to 160