Slot not invoked when sending signal with enum values
-
Hi, I have a C++ enum (backend code) that I am passing to a QML (front end) using signal/Slot connection. My post is a bit lengthy. But here is my code. For some reason, the slot is not executing. Am I missing anything here? There is no build errors. No run time error either.
File: FileEnum.h: Here is the C++ enum
namespace BatteryChargeLevel {
Q_NAMESPACE enum class BattLvl { INSUFFICIENT = 1, LOW, MODERATE, FULL, }; Q_ENUM_NS(BattLvl)}
File: File2.cpp: This is my main entry function
qRegisterMetaType<BatteryChargeLevel::BattLvl>("BatteryLvl"); qmlRegisterUncreatableMetaObject(BatteryChargeLevel::staticMetaObject, "com.mmt.components", 1, 0, "BatteryChargeLevel", "Error: only enums"); QObject *topBar = hmi->findChild<QObject *>("topBarObject"); if (!topBar) { qDebug("Couldn't find top bar object"); }:
:
QObject::connect(Obj1, SIGNAL(BatterySoC(BatteryChargeLevel::BattLvl)), topBar, SLOT(onBatterySoC(BatteryChargeLevel::BattLvl)));File3: StatusBar.qml
import com.mmt.components
id: bar objectName: "topBarObject" function onBatterySoC(BTSoC) { if(BTSoC === BatteryChargeLevel.INSUFFICIENT) { batteryIcon.source = "/Images/battery_icon_1.png" } else if (BTSoC === BatteryChargeLevel.LOW) { batteryIcon.source = "/Images/battery_icon_2.png" } else if (BTSoC === BatteryChargeLevel.MODERATE) { batteryIcon.source = "/Images/battery_icon_3.png" } else if (BTSoC === BatteryChargeLevel.FULL) { batteryIcon.source = "/Images/battery_icon_4.png" } -
QObject::connect(Obj1, SIGNAL(BatterySoC(BatteryChargeLevel::BattLvl)), topBar, SLOT(onBatterySoC(BatteryChargeLevel::BattLvl)));
Use functor-based connect syntax if possilble. It will catch more errors during compilation time.
Is
topBarandid: barthe same object? Does it haveonBatterySoCsignal or slot? -
QObject::connect(Obj1, SIGNAL(BatterySoC(BatteryChargeLevel::BattLvl)), topBar, SLOT(onBatterySoC(BatteryChargeLevel::BattLvl)));
Use functor-based connect syntax if possilble. It will catch more errors during compilation time.
Is
topBarandid: barthe same object? Does it haveonBatterySoCsignal or slot?@sierdzio said in Slot not invoked when sending signal with enum values:
Use functor-based connect syntax if possilble. It will catch more errors during compilation time.
not possible in the way @Subbu is building his program. The Slot does not exist yet at compile time
-
@sierdzio said in Slot not invoked when sending signal with enum values:
Use functor-based connect syntax if possilble. It will catch more errors during compilation time.
not possible in the way @Subbu is building his program. The Slot does not exist yet at compile time
@J-Hilk said in Slot not invoked when sending signal with enum values:
@sierdzio said in Slot not invoked when sending signal with enum values:
Use functor-based connect syntax if possilble. It will catch more errors during compilation time.
not possible in the way @Subbu is building his program. The Slot does not exist yet at compile time
You mean the JS function is the slot? I doubt something like this will work at all.
-
@J-Hilk said in Slot not invoked when sending signal with enum values:
@sierdzio said in Slot not invoked when sending signal with enum values:
Use functor-based connect syntax if possilble. It will catch more errors during compilation time.
not possible in the way @Subbu is building his program. The Slot does not exist yet at compile time
You mean the JS function is the slot? I doubt something like this will work at all.
@sierdzio said in Slot not invoked when sending signal with enum values:
You mean the JS function is the slot
yes.
I doubt something like this will work at all
I think it can, but it's highly discourage to do it this way.
Can't remember the reason.https://doc.qt.io/qt-6/qtqml-cppintegration-interactqmlfromcpp.html#accessing-loaded-qml-objects-by-object-name
I'll try to look it up.
accidentally found the issue of the op:
function onBatterySoC(BTSoC)
BTSoC is of type var -> the c++ signal argument must be of type QVarianthttps://doc.qt.io/qt-6/qtqml-cppintegration-interactqmlfromcpp.html#invoking-qml-methods