Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Slot not invoked when sending signal with enum values

Slot not invoked when sending signal with enum values

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
5 Posts 3 Posters 708 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Subbu
    wrote on last edited by Subbu
    #1

    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"
        }
    
    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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 topBar and id: bar the same object? Does it have onBatterySoC signal or slot?

      (Z(:^

      J.HilkJ 1 Reply Last reply
      1
      • sierdzioS sierdzio

        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 topBar and id: bar the same object? Does it have onBatterySoC signal or slot?

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @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


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        sierdzioS 1 Reply Last reply
        0
        • J.HilkJ J.Hilk

          @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

          sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          @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.

          (Z(:^

          J.HilkJ 1 Reply Last reply
          0
          • sierdzioS sierdzio

            @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.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by J.Hilk
            #5

            @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.
            I'll try to look it up.
            https://doc.qt.io/qt-6/qtqml-cppintegration-interactqmlfromcpp.html#accessing-loaded-qml-objects-by-object-name


            accidentally found the issue of the op:
            function onBatterySoC(BTSoC)
            BTSoC is of type var -> the c++ signal argument must be of type QVariant

            https://doc.qt.io/qt-6/qtqml-cppintegration-interactqmlfromcpp.html#invoking-qml-methods


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved