fix lcd display, do not create multiple Calculator objects (and threads)
This commit is contained in:
parent
a37b754dd5
commit
9484dc01fc
@ -8,13 +8,14 @@ Calculator::Calculator(QObject *parent) :
|
|||||||
m_lcdText("calculator initial text"),
|
m_lcdText("calculator initial text"),
|
||||||
m_timer(this)
|
m_timer(this)
|
||||||
{
|
{
|
||||||
NewKeyEmpty = 1;
|
|
||||||
qDebug() << "Starting calculator thread";
|
qDebug() << "Starting calculator thread";
|
||||||
calc_thread.start();
|
calc_thread.start();
|
||||||
qDebug() << "calculator thread started";
|
qDebug() << "calculator thread started";
|
||||||
|
|
||||||
QObject::connect(&m_timer, &QTimer::timeout, this, &Calculator::updateLcd);
|
QObject::connect(&m_timer, &QTimer::timeout, this, &Calculator::updateLcd);
|
||||||
m_timer.start(50);
|
m_timer.start(200);
|
||||||
|
|
||||||
|
updateLcd();
|
||||||
}
|
}
|
||||||
|
|
||||||
Calculator::~Calculator(){
|
Calculator::~Calculator(){
|
||||||
@ -51,16 +52,21 @@ void Calculator::updateLcd() {
|
|||||||
QString tmp("lcd text:\n");
|
QString tmp("lcd text:\n");
|
||||||
const char* lcd_buf = get_lcd_buf();
|
const char* lcd_buf = get_lcd_buf();
|
||||||
for (int i = 0; i < MAX_ROWS; i++){
|
for (int i = 0; i < MAX_ROWS; i++){
|
||||||
|
tmp += "|";
|
||||||
for (int j = 0; j < MAX_CHARS_PER_LINE; j++){
|
for (int j = 0; j < MAX_CHARS_PER_LINE; j++){
|
||||||
tmp += lcd_buf[j + i*MAX_CHARS_PER_LINE];
|
tmp += lcd_buf[j + i*MAX_CHARS_PER_LINE];
|
||||||
}
|
}
|
||||||
tmp += '\n';
|
tmp += "|\n";
|
||||||
}
|
}
|
||||||
// qDebug() << "update lcd:" << tmp.toStdString().c_str();
|
// qDebug() << "update lcd:" << tmp.toStdString().c_str();
|
||||||
|
|
||||||
setLcdText(tmp);
|
setLcdText(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Calculator::setLcdText(const QString &lcdText){
|
||||||
|
m_lcdText = lcdText;
|
||||||
|
emit lcdTextChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ public:
|
|||||||
~Calculator();
|
~Calculator();
|
||||||
|
|
||||||
inline QString lcdText(){return m_lcdText;}
|
inline QString lcdText(){return m_lcdText;}
|
||||||
inline void setLcdText(const QString &lcdText){m_lcdText = lcdText;}
|
void setLcdText(const QString &lcdText);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void lcdTextChanged();
|
void lcdTextChanged();
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QObject>
|
#include <QFontDatabase>
|
||||||
#include <QQmlApplicationEngine>
|
#include <QQmlApplicationEngine>
|
||||||
#include <QQmlContext>
|
#include <QQmlContext>
|
||||||
#include "calculator.h"
|
#include "calculator.h"
|
||||||
@ -8,13 +8,15 @@ int main(int argc, char** argv)
|
|||||||
{
|
{
|
||||||
QApplication app(argc, argv);
|
QApplication app(argc, argv);
|
||||||
|
|
||||||
qmlRegisterType<Calculator>("calculator.lcd", 1, 0, "CLcd");
|
|
||||||
|
|
||||||
QQmlApplicationEngine engine;
|
QQmlApplicationEngine engine;
|
||||||
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
|
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
|
||||||
|
|
||||||
Calculator calculator;
|
Calculator calculator;
|
||||||
engine.rootContext()->setContextProperty("_calculator", &calculator);
|
engine.rootContext()->setContextProperty("_calculator", &calculator);
|
||||||
|
|
||||||
|
QFont fixedFont = QFontDatabase::systemFont(QFontDatabase::FixedFont);
|
||||||
|
fixedFont.setStyleHint(QFont::TypeWriter);
|
||||||
|
engine.rootContext()->setContextProperty("_fixedFont", fixedFont);
|
||||||
|
|
||||||
return app.exec();
|
return app.exec();
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import QtQuick 2.0
|
import QtQuick 2.0
|
||||||
import QtQuick.Controls 1.0
|
import QtQuick.Controls 1.0
|
||||||
import calculator.lcd 1.0
|
|
||||||
|
|
||||||
ApplicationWindow
|
ApplicationWindow
|
||||||
{
|
{
|
||||||
@ -28,11 +27,6 @@ ApplicationWindow
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//lcd text from C++
|
|
||||||
CLcd {
|
|
||||||
id: clcd
|
|
||||||
}
|
|
||||||
|
|
||||||
//LCD
|
//LCD
|
||||||
Rectangle {
|
Rectangle {
|
||||||
objectName: "lcd";
|
objectName: "lcd";
|
||||||
@ -41,10 +35,9 @@ ApplicationWindow
|
|||||||
height: 200
|
height: 200
|
||||||
Text {
|
Text {
|
||||||
objectName: "lcd_text";
|
objectName: "lcd_text";
|
||||||
text: clcd.lcdText
|
text: _calculator.lcdText
|
||||||
|
font: _fixedFont
|
||||||
anchors.centerIn: parent
|
anchors.centerIn: parent
|
||||||
|
|
||||||
onTextChanged: clcd.lcdText = text
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user