Cleanup datatypes.
This commit is contained in:
76
main.cpp
76
main.cpp
@@ -31,6 +31,8 @@
|
||||
#include <QApplication>
|
||||
#include <QMetaEnum>
|
||||
#include <QFile>
|
||||
#include <QLabel>
|
||||
#include <QProgressBar>
|
||||
|
||||
// main.cpp
|
||||
#include <QCoreApplication>
|
||||
@@ -41,25 +43,8 @@
|
||||
#include <bcdataitem.h>
|
||||
#include <bcvdatamanager.h>
|
||||
|
||||
/*
|
||||
#include <QMetaEnum>
|
||||
|
||||
void parseString(const QString &inputString) {
|
||||
QMetaEnum metaEnum = QMetaEnum::fromType<BCData::ID>();
|
||||
|
||||
bool ok = false;
|
||||
// keyToValue parst den String ("x1") und liefert den int-Wert
|
||||
int intVal = metaEnum.keyToValue(inputString.toLatin1().constData(), &ok);
|
||||
|
||||
if (ok) {
|
||||
BCData::ID id = static_cast<BCData::ID>(intVal);
|
||||
// Erfolg!
|
||||
} else {
|
||||
// Fehler: String existiert nicht im Enum
|
||||
qWarning() << "Unbekannter Enum String:" << inputString;
|
||||
}
|
||||
}
|
||||
*/
|
||||
#include <variant>
|
||||
#include <string>
|
||||
|
||||
|
||||
|
||||
@@ -81,6 +66,59 @@ bool setApplicationStyleSheet( QAnyStringView path )
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
struct moo
|
||||
{
|
||||
QString moo_str {"irgendwas"};
|
||||
};
|
||||
|
||||
// Verschiedene Datentypen für die Sensoren
|
||||
struct Temperature : public moo{ float celsius; };
|
||||
struct CpuLoad : public moo{ int percentage; };
|
||||
struct Status : public moo{ std::string message; };
|
||||
|
||||
// Die Variant als universeller Datencontainer
|
||||
using SensorReading = std::variant<Temperature, CpuLoad, Status>;
|
||||
|
||||
|
||||
|
||||
struct UIUpdateVisitor
|
||||
{
|
||||
QLabel* tempLabel;
|
||||
QProgressBar* cpuBar;
|
||||
QLabel* statusLabel;
|
||||
|
||||
// Überladene operator() für jeden Typ in der Variant
|
||||
void operator()(const Temperature& t) const
|
||||
{
|
||||
tempLabel->setText(QString("Temp: %1°C").arg(t.celsius, 0, 'f', 1));
|
||||
tempLabel->setStyleSheet(t.celsius > 40 ? "color: red;" : "color: black;");
|
||||
}
|
||||
|
||||
void operator()(const CpuLoad& c) const
|
||||
{
|
||||
cpuBar->setValue(c.percentage);
|
||||
qDebug() << c.moo_str;
|
||||
}
|
||||
|
||||
void operator()(const Status& s) const
|
||||
{
|
||||
statusLabel->setText(QString::fromStdString(s.message));
|
||||
}
|
||||
};
|
||||
|
||||
void onNewDataReceived(const SensorReading& data)
|
||||
{
|
||||
QLabel* labelTemp = nullptr;
|
||||
QProgressBar* progressCpu = nullptr;
|
||||
QLabel* labelStatus = nullptr;
|
||||
|
||||
// Wir erstellen den Visitor mit Referenzen auf unsere UI-Elemente
|
||||
UIUpdateVisitor visitor { labelTemp, progressCpu, labelStatus };
|
||||
|
||||
// Die Magie: std::visit wählt zur Kompilierzeit die richtige Methode
|
||||
std::visit(visitor, data);
|
||||
}
|
||||
// 2. Datei öffnen und lesen
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user