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:


    #include "myserver.h"
    #include <QSignalMapper>
    #include <QTcpServer>
    #include <QTcpSocket>
    #include <QQmlApplicationEngine>
    #include <QQmlComponent>
    MyServer::MyServer(QObject *parent) : QObject(parent)
        QQmlApplicationEngine engine;
        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();
    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()


    #include <QApplication>
    #include <QQmlApplicationEngine>
    #include <myserver.h>
    int main(int argc, char *argv[])
        QApplication app(argc, argv);
        QQmlApplicationEngine engine;
        MyServer mServer;
        return app.exec();


    #ifndef MYSERVER_H
    #define MYSERVER_H
    #include <QObject>
    #include <QDebug>
    #include <QTcpServer>
    #include <QTcpSocket>
    #include <QHostAddress>
    #include <QObject>
    #include <QSignalMapper>
    class MyServer : public QObject
        explicit MyServer(QObject *parent = 0);
        void sendData(std::string data);
        void dataRecievedFromServerApp(QByteArray data);
    public slots:
        void newConnection();
        void readyRead();
        void onDisconnected();
        QTcpServer *server;
        QTcpSocket *socket;
        QObject *GUI;
        QSignalMapper *m_readyReadSignalMapper;
        QSignalMapper *m_disconnectedSignalMapper;
    #endif // MYSERVER_H

  • Moderators

    @antemort So what is the error ? Are you able to find that property ?

  • @p3c0 the error is Component is not ready and when I try to change it crashes :(

  • Moderators

    @antemort That means there is some error in the QML due to which the engine is not able to load it.

  • 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

  • Moderators

    @antemort First check if that QML contains no error. Secondly you need to findChild in the rootObject

    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

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.