Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. connecting C++ slot to signal from QML Component
Forum Updated to NodeBB v4.3 + New Features

connecting C++ slot to signal from QML Component

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 223 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    Hi all -

    On this page, there's a good description of connecting a QML signal to a C++ slot, providing that the QML component is created within C++. I need to connect a C++ slot to a signal coming from a QML object that isn't defined by the C++ class with the slot.

    Here's a snippet of my QML:

    GridView {
        id: equipmentView
    
        // the model selection is complex, but it works.
        model: (zoneModel.listSize() > 0)
               ? (zoneSelected === 0)
                 ? equipmentModel.uuidList
                 : (zoneSelected > 0 && zoneModel.listSize() > zoneSelected)
                   ? zoneModel.getZone(zoneSelected).equipmentList
                   : null
               : null
    
        delegate: Item {
            id: card
            property string uuid: equipmentModel.getEquipmentItem(model.modelData).uuid()
            signal switchClicked(string uuid, bool on)
    
            Switch {
                id: onOffSwitch
                onClicked: {
                    card.switchClicked(card.uuid, onOffSwitch.checked)
                }
            }
        }
    }
    

    Is it possible to connect this signal to a C++ slot, or do I need to create this QML entity from with C++ in order to do so?

    Thanks...

    GrecKoG 1 Reply Last reply
    0
    • mzimmersM mzimmers

      Hi all -

      On this page, there's a good description of connecting a QML signal to a C++ slot, providing that the QML component is created within C++. I need to connect a C++ slot to a signal coming from a QML object that isn't defined by the C++ class with the slot.

      Here's a snippet of my QML:

      GridView {
          id: equipmentView
      
          // the model selection is complex, but it works.
          model: (zoneModel.listSize() > 0)
                 ? (zoneSelected === 0)
                   ? equipmentModel.uuidList
                   : (zoneSelected > 0 && zoneModel.listSize() > zoneSelected)
                     ? zoneModel.getZone(zoneSelected).equipmentList
                     : null
                 : null
      
          delegate: Item {
              id: card
              property string uuid: equipmentModel.getEquipmentItem(model.modelData).uuid()
              signal switchClicked(string uuid, bool on)
      
              Switch {
                  id: onOffSwitch
                  onClicked: {
                      card.switchClicked(card.uuid, onOffSwitch.checked)
                  }
              }
          }
      }
      

      Is it possible to connect this signal to a C++ slot, or do I need to create this QML entity from with C++ in order to do so?

      Thanks...

      GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      @mzimmers Do you know which c++ object you want to connect to?

      Can't you do: onSwitchClicked: (uuid, on) => cppObject.activateEquipment(uuid, on)?

      mzimmersM 1 Reply Last reply
      3
      • GrecKoG GrecKo

        @mzimmers Do you know which c++ object you want to connect to?

        Can't you do: onSwitchClicked: (uuid, on) => cppObject.activateEquipment(uuid, on)?

        mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by mzimmers
        #3

        @GrecKo yes I know which C++ object (it's the instantiation of my EquipmentModel, of which there will be only one). So...how do I represent the C++ object in the QML?

        And, not that it would be a problem, but...is your example a signal/slot usage, or are you just calling a C++ method?

        EDIT: holy cow...it actually works!

        onSwitchClicked: (uuid, onOff) => {
                             equipmentModel.receiveQmlSignal(uuid, onOff)
                         }
        

        I didn't realize you could do that. Wow...mind blown.

        Thanks, @GrecKo.

        1 Reply Last reply
        0
        • mzimmersM mzimmers has marked this topic as solved on

        • Login

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