Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. c++ and qml .. modifying properties need help
Forum Updated to NodeBB v4.3 + New Features

c++ and qml .. modifying properties need help

Scheduled Pinned Locked Moved QML and Qt Quick
qtcreatorqmlcpp
6 Posts 2 Posters 2.0k Views 2 Watching
  • 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.
  • A Offline
    A Offline
    antemort
    wrote on last edited by antemort
    #1

    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
    
    p3c0P 1 Reply Last reply
    0
    • A antemort

      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
      
      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

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

      157

      A 1 Reply Last reply
      0
      • p3c0P p3c0

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

        A Offline
        A Offline
        antemort
        wrote on last edited by
        #3

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

        p3c0P 1 Reply Last reply
        0
        • A antemort

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

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

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

          157

          1 Reply Last reply
          0
          • A Offline
            A Offline
            antemort
            wrote on last edited by
            #5

            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

            p3c0P 1 Reply Last reply
            0
            • A antemort

              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

              p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              @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
              

              157

              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