From 7d11099e5afd1f16c811ccbe954a158105be36ab Mon Sep 17 00:00:00 2001 From: Daniel Teichmann Date: Sat, 18 Jul 2020 16:45:50 +0200 Subject: Add pinChange() function --- main.cpp | 33 +++++++++++++++++++++++++++++++++ main.qml | 10 ++++++++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/main.cpp b/main.cpp index 6889e8d..091f649 100644 --- a/main.cpp +++ b/main.cpp @@ -1,8 +1,14 @@ #include #include +#include +#include +#include #include #include +void changePin(QString pin, QQmlApplicationEngine &engine); +void gen_random(char *s, const int len); + int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); @@ -25,5 +31,32 @@ int main(int argc, char *argv[]) if (engine.rootObjects().isEmpty()) return -1; + #define PIN_LENGTH 5 + // The char array in which the random pin will be written to + char rand_pin[PIN_LENGTH+1]; + gen_random(rand_pin, PIN_LENGTH); + changePin(rand_pin, engine); + return app.exec(); } + +void changePin(QString pin, QQmlApplicationEngine &engine){ + QQuickItem *item = engine.rootObjects().at(0)->findChild("pin_text"); + if (item) + item->setProperty("pin", pin); +} + +void gen_random(char *s, const int len) { + // Initialize random generator + srand( static_cast(time(NULL))); + + static const char alphanum[] = + "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + for (int i = 0; i < len; ++i) { + s[i] = alphanum[rand() % static_cast(sizeof(alphanum) - 1)]; + } + + s[len] = 0; +} diff --git a/main.qml b/main.qml index 20a1da6..1e6b242 100644 --- a/main.qml +++ b/main.qml @@ -84,8 +84,14 @@ Window { id: pin_text x: 235 width: 210 - height: 50 - text: qsTr("0 1 A 2 3 B") + height: 28 + // ':' translation info + //: Do not translate! + //: This is just the pin which is used to authenticate to remote web app (django) frontend + text: qsTr("%1").arg(pin) + property string pin: "-----" + font.letterSpacing: 10 + anchors.horizontalCenterOffset: 0 font.pixelSize: 26 anchors.horizontalCenter: your_pin_text.horizontalCenter anchors.top: your_pin_text.bottom -- cgit v1.2.3