Modify a QML Text from C++



  • 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
        }
    
    }
    


  • 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
            }
    }
    


  • @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.



  • @BrianDavis Thank you


Log in to reply
 

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