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. Modify a QML Text from C++
Forum Updated to NodeBB v4.3 + New Features

Modify a QML Text from C++

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmlc++
4 Posts 2 Posters 3.6k 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.
  • N Offline
    N Offline
    neda
    wrote on last edited by neda
    #1

    Hi,
    I'm using Qt version 5.5.1 from windows 8.1.
    I can not bind data to text control.
    My program stopped working and the result was this error:

    Unable to assign QQuickText to QString
    The program has unexpectedly finished.
    

    Please guide me.
    Thanks

    main.cpp:

    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
        MySerialPort iSerialPort;
        iSerialPort.myText= engine.rootObjects().at(0)->findChild<QObject*>("text1Text");    
        iSerialPort.openSerialPort();
    	
    	QThread* thread;
    	iSerialPort.moveToThread(thread);
        thread->start();
    	
        return app.exec();
    }
    
    
    

    serialport.cpp:

    void MySerialPort::readData()
    {
        QByteArray data = serial->readAll();
    
        qDebug() << data;
    
    
        myText->setProperty("text", data);
    }
    

    myserialport.h:

    #ifndef MYSERIALPORT_H
    #define MYSERIALPORT_H
    #include <QtSerialPort/QtSerialPort>
    #include <QObject>
    #include <QApplication>
    #include <QQmlApplicationEngine>
    
    class MySerialPort: public QSerialPort
    {
         Q_OBJECT
    public:
        MySerialPort();
        QObject *myText;
    
    public slots:
        void openSerialPort();
        void closeSerialPort();
    
        void writeData(const QByteArray &data);
        void readData();
    
        void handleError(QSerialPort::SerialPortError error);
    
    private:
        void showStatusMessage(const QString &message);
    
        QSerialPort *serial;
    
    };
    
    #endif // MYSERIALPORT_H
    

    main.qml:

    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
    
        Text {
            id: text1Text
            objectName: text1Text
            width: 400
            height: 29
            color: "red"
            text: "This text should change..."
            font.pixelSize: 12
        }
    
    }
    
    B 1 Reply Last reply
    0
    • N Offline
      N Offline
      neda
      wrote on last edited by neda
      #2

      My problem is solved.
      But
      Is this the best code?
      What do you think?
      Can you suggest a better method?

      main.cpp:

      #include <QApplication>
      #include <QQmlApplicationEngine>
      #include <myserialport.h>
      #include <QQmlContext>
      
      int main(int argc, char *argv[])
      {
          QApplication app(argc, argv);
      
          QQmlApplicationEngine engine;
          engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
      
          MyDisplay myDisplay;
      	myDisplay.setText("neda");
          engine.rootContext()->setContextProperty("myDisplay", &myDisplay);
      	
          MySerialPort iSerialPort;
       iSerialPort.setDisplay(&myDisplay);
          iSerialPort.openSerialPort();
      
          return app.exec();
      }
      

      mydisplay.h:

      #include <QObject>
      
      #ifndef MYDISPLAY_H
      #define MYDISPLAY_H
      
      class MyDisplay : public QObject
      {
          Q_OBJECT
          Q_PROPERTY(QString newText READ getText WRITE setText NOTIFY textChanged)
      
      public:
          MyDisplay();
      
          MyDisplay(QString);
      
          Q_INVOKABLE QString getText() const;
      public slots:
          void setText(QString text);
      
      signals:
          void textChanged(QString);
      
      private:
          QString newText;
      };
      
      #endif // MYDISPLAY_H
      

      mydisplay.cpp:

      #include "mydisplay.h"
      #include "qstring.h"
      MyDisplay::MyDisplay()
      {
          newText = "";
      }
      
      MyDisplay::MyDisplay(QString text)
      {
          newText = text;
      }
      
      QString MyDisplay::getText() const
      {
          return newText;
      }
      
      void MyDisplay::setText(QString text)
      {
          if (text != newText)
          {
              newText = text;
              emit textChanged(text);
          }
      }
      

      serialport.cpp:

      void MySerialPort::readData()
      {
      
          QByteArray data = serial->readAll();
      
      
          qDebug() << data;
      
          QString dataString=data;
      
          myDisplay->setText(dataString);
      }
      void MySerialPort::setDisplay(MyDisplay * m_display)
      {
      myDisplay = m_display;
      }
      

      myserialport.h:

      class MySerialPort: public QSerialPort
      {
           Q_OBJECT
      public:
          MySerialPort();
      
      public slots:
          void openSerialPort();
          void closeSerialPort();
      void setDisplay(MyDisplay * m_display);
          void writeData(const QByteArray &data);
          void readData();
      
          void handleError(QSerialPort::SerialPortError error);
      
      private:
          void showStatusMessage(const QString &message);
          MyDisplay * myDisplay;
          QSerialPort *serial;
      
      };
      

      main.qml:

      ApplicationWindow {
          visible: true
          width: 640
          height: 480
          title: qsTr("Hello World")
      
          Text {
                  id: text1Text
                  //objectName: text1Text
                  width: 400
                  height: 29
                  color: "red"
                  text: myDisplay.newText
                  font.pixelSize: 12
              }
      }
      
      1 Reply Last reply
      0
      • N neda

        Hi,
        I'm using Qt version 5.5.1 from windows 8.1.
        I can not bind data to text control.
        My program stopped working and the result was this error:

        Unable to assign QQuickText to QString
        The program has unexpectedly finished.
        

        Please guide me.
        Thanks

        main.cpp:

        int main(int argc, char *argv[])
        {
            QApplication app(argc, argv);
        
            QQmlApplicationEngine engine;
            engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        
            MySerialPort iSerialPort;
            iSerialPort.myText= engine.rootObjects().at(0)->findChild<QObject*>("text1Text");    
            iSerialPort.openSerialPort();
        	
        	QThread* thread;
        	iSerialPort.moveToThread(thread);
            thread->start();
        	
            return app.exec();
        }
        
        
        

        serialport.cpp:

        void MySerialPort::readData()
        {
            QByteArray data = serial->readAll();
        
            qDebug() << data;
        
        
            myText->setProperty("text", data);
        }
        

        myserialport.h:

        #ifndef MYSERIALPORT_H
        #define MYSERIALPORT_H
        #include <QtSerialPort/QtSerialPort>
        #include <QObject>
        #include <QApplication>
        #include <QQmlApplicationEngine>
        
        class MySerialPort: public QSerialPort
        {
             Q_OBJECT
        public:
            MySerialPort();
            QObject *myText;
        
        public slots:
            void openSerialPort();
            void closeSerialPort();
        
            void writeData(const QByteArray &data);
            void readData();
        
            void handleError(QSerialPort::SerialPortError error);
        
        private:
            void showStatusMessage(const QString &message);
        
            QSerialPort *serial;
        
        };
        
        #endif // MYSERIALPORT_H
        

        main.qml:

        ApplicationWindow {
            visible: true
            width: 640
            height: 480
            title: qsTr("Hello World")
        
        
            Text {
                id: text1Text
                objectName: text1Text
                width: 400
                height: 29
                color: "red"
                text: "This text should change..."
                font.pixelSize: 12
            }
        
        }
        
        B Offline
        B Offline
        BrianDavis
        wrote on last edited by
        #3

        @neda What was the data type of myText in serialport.h?
        I have had luck with passing the result of

        findChild<QQuickItem*>("Lorem")
        

        to another class, and then using

        setProperty("text", "Ipsum")
        

        In some cases you may even find it valuable to check that findChild returns what you expected

        if (theFoundChild && theFoundChild->inherits("QQuickText"))
        

        The error makes it sound like you were passing a the QObject* to be stored as a QString. If that is not the case, then perhaps trying QQuickItem* instead of QObject* would help.

        I realize your question is a bit dated, but others will stumble on it as well.

        Using contextProperties as you did in your solution makes the code more difficult to understand and less encapsulated.

        N 1 Reply Last reply
        1
        • B BrianDavis

          @neda What was the data type of myText in serialport.h?
          I have had luck with passing the result of

          findChild<QQuickItem*>("Lorem")
          

          to another class, and then using

          setProperty("text", "Ipsum")
          

          In some cases you may even find it valuable to check that findChild returns what you expected

          if (theFoundChild && theFoundChild->inherits("QQuickText"))
          

          The error makes it sound like you were passing a the QObject* to be stored as a QString. If that is not the case, then perhaps trying QQuickItem* instead of QObject* would help.

          I realize your question is a bit dated, but others will stumble on it as well.

          Using contextProperties as you did in your solution makes the code more difficult to understand and less encapsulated.

          N Offline
          N Offline
          neda
          wrote on last edited by
          #4

          @BrianDavis Thank you

          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