My problem is solved.
Thanks
main.cpp:
#include <QApplication>
#include <QQmlApplicationEngine>
#include <myserialport.h>
#include <QQmlContext>
int main(int argc, char *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();
}
#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();
}
To copy to clipboard, switch view to plain text mode
mydisplay.h:
#include <QObject>
#ifndef MYDISPLAY_H
#define MYDISPLAY_H
{
Q_OBJECT
Q_PROPERTY(QString newText READ getText WRITE setText NOTIFY textChanged
)
public:
MyDisplay();
Q_INVOKABLE
QString getText
() const;
public slots:
signals:
private:
};
#endif // 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
To copy to clipboard, switch view to plain text mode
mydisplay.cpp:
#include "mydisplay.h"
#include "qstring.h"
MyDisplay::MyDisplay()
{
newText = "";
}
{
newText = text;
}
{
return newText;
}
void MyDisplay
::setText(QString text
) {
if (text != newText)
{
newText = text;
emit textChanged(text);
}
}
#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);
}
}
To copy to clipboard, switch view to plain text mode
serialport.cpp:
void MySerialPort::readData()
{
qDebug() << data;
myDisplay->setText(dataString);
}
void MySerialPort::setDisplay(MyDisplay * m_display)
{
myDisplay = m_display;
}
void MySerialPort::readData()
{
QByteArray data = serial->readAll();
qDebug() << data;
QString dataString=data;
myDisplay->setText(dataString);
}
void MySerialPort::setDisplay(MyDisplay * m_display)
{
myDisplay = m_display;
}
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: myDisplay.newText
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: myDisplay.newText
font.pixelSize: 12
}
}
To copy to clipboard, switch view to plain text mode
myserialport.h
class MySerialPort: public QSerialPort
{
Q_OBJECT
public:
MySerialPort();
public slots:
void openSerialPort();
void closeSerialPort();
void setDisplay(MyDisplay * m_display);
void readData();
void handleError(QSerialPort::SerialPortError error);
private:
void showStatusMessage
(const QString &message
);
MyDisplay * myDisplay;
QSerialPort *serial;
};
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;
};
To copy to clipboard, switch view to plain text mode
Bookmarks