fix lcd display, do not create multiple Calculator objects (and threads)

This commit is contained in:
Jeff Wang 2019-04-04 01:32:15 -04:00
parent a37b754dd5
commit 9484dc01fc
4 changed files with 17 additions and 16 deletions

View File

@ -8,13 +8,14 @@ Calculator::Calculator(QObject *parent) :
m_lcdText("calculator initial text"),
m_timer(this)
{
NewKeyEmpty = 1;
qDebug() << "Starting calculator thread";
calc_thread.start();
qDebug() << "calculator thread started";
QObject::connect(&m_timer, &QTimer::timeout, this, &Calculator::updateLcd);
m_timer.start(50);
m_timer.start(200);
updateLcd();
}
Calculator::~Calculator(){
@ -51,16 +52,21 @@ void Calculator::updateLcd() {
QString tmp("lcd text:\n");
const char* lcd_buf = get_lcd_buf();
for (int i = 0; i < MAX_ROWS; i++){
tmp += "|";
for (int j = 0; j < MAX_CHARS_PER_LINE; j++){
tmp += lcd_buf[j + i*MAX_CHARS_PER_LINE];
}
tmp += '\n';
tmp += "|\n";
}
// qDebug() << "update lcd:" << tmp.toStdString().c_str();
setLcdText(tmp);
}
void Calculator::setLcdText(const QString &lcdText){
m_lcdText = lcdText;
emit lcdTextChanged();
}

View File

@ -29,7 +29,7 @@ public:
~Calculator();
inline QString lcdText(){return m_lcdText;}
inline void setLcdText(const QString &lcdText){m_lcdText = lcdText;}
void setLcdText(const QString &lcdText);
signals:
void lcdTextChanged();

View File

@ -1,5 +1,5 @@
#include <QApplication>
#include <QObject>
#include <QFontDatabase>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "calculator.h"
@ -8,13 +8,15 @@ int main(int argc, char** argv)
{
QApplication app(argc, argv);
qmlRegisterType<Calculator>("calculator.lcd", 1, 0, "CLcd");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
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();
}

View File

@ -1,6 +1,5 @@
import QtQuick 2.0
import QtQuick.Controls 1.0
import calculator.lcd 1.0
ApplicationWindow
{
@ -27,11 +26,6 @@ ApplicationWindow
anchors.fill: parent
}
}
//lcd text from C++
CLcd {
id: clcd
}
//LCD
Rectangle {
@ -41,10 +35,9 @@ ApplicationWindow
height: 200
Text {
objectName: "lcd_text";
text: clcd.lcdText
text: _calculator.lcdText
font: _fixedFont
anchors.centerIn: parent
onTextChanged: clcd.lcdText = text
}
}