Why it doesnt emit ?
-
I m not able to understand why my code doents emit.
My task is: reading battery level and showing immagine with battery level.
So this i my code:
battery.ccp
Battery::Battery(QObject *parent) : QObject(parent), timer{ new QTimer(this) } { qDebug()<<"class Battery"; connect(timer, &QTimer::timeout, this, &Battery::checkBatteryLevel); timer->start(std::chrono::milliseconds(2000)); } void Battery::checkBatteryLevel() { QFile batteryFile(BATTERY_FILE_NAME); if (batteryFile.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug()<<"file open"; } else { qDebug()<<"file not open"; return; } if (QTextStream(&batteryFile).readAll().toInt() < 25) { qDebug() << "first level battery"; emit batteryLevel(1); } if (QTextStream(&batteryFile).readAll().toInt() > 25) { qDebug()<< "second level battery"; emit batteryLevel(2); } }
main.qml
ApplicationWindow { id: applicationWindow visible: true width: 419 height: 138 color: "black" // the current date and time property date now: null property int index: 0 property var slidesPath: [ "qrc:/res/icons/low_batt.png", "qrc:/res/icons/med_batt.png", "qrc:/res/icons/med_high_battery.png", "qrc:/res/icons/icons/no_app_found.png" ] Image { id: ic width: 90 y: 22 fillMode: Image.PreserveAspectFit source: slidesPath[index] // height: width anchors.left: parent.left anchors.leftMargin: 20 anchors.verticalCenter: parent.verticalCenter } Text { y: 60 text: new Date().toLocaleTimeString(Qt.LocaleDate) // new Date().toLocaleDateString(Qt.locale("it_IT")) // anchors.left: ic.right anchors.leftMargin: 1 anchors.top: parent.top anchors.topMargin: 10 anchors.verticalCenter: parent.verticalCenter font.bold: true font.pointSize: 14 color: "white" } Text { y: 60 text: new Date().toLocaleDateString(Qt.locale("en_EN")) // anchors.left: ic.right anchors.leftMargin: 1 anchors.top: parent.top anchors.topMargin: 30 anchors.verticalCenter: parent.verticalCenter font.bold: true font.pointSize: 14 color: "white" } Text { id: txt text: "nome app variabile" height: 50 font.capitalization: Font.AllUppercase color: "white" font.bold: true font.pointSize: 18 //36 anchors.left: ic.right anchors.leftMargin: 1 //20 anchors.topMargin: 70 anchors.verticalCenter: parent.verticalCenter verticalAlignment: Text.AlignVCenter } Text { text: qsTr(".") anchors.centerIn: parent focus: true Keys.onPressed: { if (event.key === Qt.Key_Up) { if (index < slidesPath.length - 1) index++; process.start("c:\\windows\\notepad.exe"); process.waitForFinished(); index = 0; } else if (event.key === Qt.Key_Down) { if (index > 0) index--; else index = 3; } } } Battery { id: batteryLevel // swipeView.currentIndex--; onValueChanged: { console.log("battery "); switch (percent) {case 1: slidesPath[2]; // source: "qrc:/res/icons/low_batt.png" console.log("case 1 "); break; case 2: applicationWindow.index=3; console.log("case 2 "); // source: "qrc:/res/icons/med_batt.png" break; } }
My logs are:
class Battery
file open
first level battery
file open
first level battery
......... -
Hi,
Beside the good suggestion of @fcarney, you might be in the unlucky case of the battery value being 25 thus nothing will be emitted.
On a side note, it would make your code way more clean, readable and error proof to read and convert the value once (doing a conversion check would be good as well) and then apply your logic. You currently create two streams and read the file twice which is a bit inefficient and not clean from a maintenance point of view.
-
@Montanaro said in Why it doesnt emit ?:
I dont understand what you suggest
In your code you clearly read the content of the file two times:
if (QTextStream(&batteryFile).readAll().toInt() < 25) { qDebug() << "first level battery"; emit batteryLevel(1); } if (QTextStream(&batteryFile).readAll().toInt() > 25) { qDebug()<< "second level battery"; emit batteryLevel(2); }
It is enough and also faster to do it once. And toInt() has a bool parameter to check whther the content of the string could be converted to an int (if the string does not contain valid integer number toInt() will fail):
bool ok{false}; int value = QTextStream(&batteryFile).readAll().toInt(&ok); if (!ok) { // Handle the error here } if (value < 25) { qDebug() << "first level battery"; emit batteryLevel(1); } if (value > 25) { qDebug()<< "second level battery"; emit batteryLevel(2); }
-
@Montanaro said in Why it doesnt emit ?:
I tryed to change "unsolved tag" into "solved tag but" but I m not able :/
bottom right,
topic tools
->mark as solved