Results 1 to 8 of 8

Thread: modify a QML Text from C++

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: modify a QML Text from C++

    Quote Originally Posted by anda_skoa View Post
    MySerialPort::readData() runs in a worker thread, it can't instantiate UI elements.

    The cleanest way would be to have a model for the strings and use a Repeater in your main.qml to create MyItem instances for each model entry.

    The model would need a slot that takes a string and appends it to its data.

    You would then connect this slot to a signal in your serial port class which is emitted when it has received a new string.

    Cheers,
    _
    Thanks for your reply.
    Please guide me with simple code.

    I change my code but I don't see any change to text control.
    main.cpp:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. QQmlApplicationEngine engine;
    6. engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    7.  
    8. MySerialPort iSerialPort;
    9. QThread* thread = new QThread(app);
    10. iSerialPort.moveToThread(thread);
    11. thread->start();
    12. iSerialPort.myText= engine.rootObjects().at(0)->findChild<QObject*>("text1Text");
    13. iSerialPort.openSerialPort();
    14.  
    15. return app.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

    serialport.cpp:
    Qt Code:
    1. void MySerialPort::readData()
    2. {
    3. QByteArray data = serial->readAll();
    4.  
    5. qDebug() << data;
    6.  
    7.  
    8. myText->setProperty("text", data);
    9. }
    To copy to clipboard, switch view to plain text mode 

    myserialport.h
    Qt Code:
    1. #ifndef MYSERIALPORT_H
    2. #define MYSERIALPORT_H
    3. #include <QtSerialPort/QtSerialPort>
    4. #include <QObject>
    5. #include <QApplication>
    6. #include <QQmlApplicationEngine>
    7.  
    8. class MySerialPort: public QSerialPort
    9. {
    10. Q_OBJECT
    11. public:
    12. MySerialPort();
    13. QObject *myText;
    14.  
    15. public slots:
    16. void openSerialPort();
    17. void closeSerialPort();
    18.  
    19. void writeData(const QByteArray &data);
    20. void readData();
    21.  
    22. void handleError(QSerialPort::SerialPortError error);
    23.  
    24. private:
    25. void showStatusMessage(const QString &message);
    26.  
    27. QSerialPort *serial;
    28.  
    29. };
    30.  
    31. #endif // MYSERIALPORT_H
    To copy to clipboard, switch view to plain text mode 

    main.qml:
    Qt Code:
    1. ApplicationWindow {
    2. visible: true
    3. width: 640
    4. height: 480
    5. title: qsTr("Hello World")
    6.  
    7.  
    8. Text {
    9. id: text1Text
    10. objectName: text1Text
    11. width: 400
    12. height: 29
    13. color: "red"
    14. text: "This text should change..."
    15. font.pixelSize: 12
    16. }
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by neda; 14th February 2016 at 09:58.

Similar Threads

  1. Replies: 1
    Last Post: 11th May 2014, 08:29
  2. Replies: 6
    Last Post: 3rd August 2012, 06:48
  3. Replies: 1
    Last Post: 24th April 2010, 15:31
  4. How to modify the content of text file
    By grsandeep85 in forum Qt Programming
    Replies: 3
    Last Post: 31st July 2009, 10:23
  5. how to modify the text of node in xml docment?
    By GChen in forum Qt Programming
    Replies: 5
    Last Post: 26th February 2009, 10:48

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.