Originally Posted by
anda_skoa
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:
int main(int argc, char *argv[])
{
QQmlApplicationEngine engine;
engine.
load(QUrl(QStringLiteral
("qrc:/main.qml")));
MySerialPort iSerialPort;
iSerialPort.moveToThread(thread);
thread->start();
iSerialPort.myText= engine.rootObjects().at(0)->findChild<QObject*>("text1Text");
iSerialPort.openSerialPort();
return app.exec();
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
MySerialPort iSerialPort;
QThread* thread = new QThread(app);
iSerialPort.moveToThread(thread);
thread->start();
iSerialPort.myText= engine.rootObjects().at(0)->findChild<QObject*>("text1Text");
iSerialPort.openSerialPort();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
serialport.cpp:
void MySerialPort::readData()
{
qDebug() << data;
myText->setProperty("text", data);
}
void MySerialPort::readData()
{
QByteArray data = serial->readAll();
qDebug() << data;
myText->setProperty("text", data);
}
To copy to clipboard, switch view to plain text mode
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();
public slots:
void openSerialPort();
void closeSerialPort();
void readData();
void handleError(QSerialPort::SerialPortError error);
private:
void showStatusMessage
(const QString &message
);
QSerialPort *serial;
};
#endif // 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
To copy to clipboard, switch view to plain text mode
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
}
}
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
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks