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. How to pass a function as model data?
QtWS25 Last Chance

How to pass a function as model data?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmlmodelmodeldatarolefunction
4 Posts 2 Posters 2.2k Views
  • 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.
  • J Offline
    J Offline
    jdcordoba
    wrote on 7 Jun 2016, 13:12 last edited by
    #1

    How can we pass a function as data within a model ?
    I am trying this way, which doesn't work :(

    Main.qml:

    import QtQuick 2.0
    import QtQuick.Window 2.2
    Window {
        visible: true
        MyBtnRow {
            btnData:
                [
                { tag:"Jump",   action:function(){console.log (" Jump Called OK!");}  },
                { tag:"Run",     action:(function(){console.log(" Run  Called OK!");} ) },
                { tag:"Hide",    action:function(){console.log( " Hide Called OK!");}  },
            ]
        }
    }
    
    

    MyBtnRow.qml

    import QtQuick 2.0
    
    Row {
        property var btnData  // [{ tag:"Name",  action: function() {... } }
        spacing: 3
    
        Repeater {
            id: rep
            model: btnData
            Btn {
                id: btnId
                width: 60;
                height: 50;
                label.text:  "Do " + modelData.tag
                onActivated: {
                    try { modelData.action() } catch (err) {console.log("  catch (err:" +err.message+") in Button "+label.text);};
                }
            }
        }
    }
    
    
    

    Btn.qml

    import QtQuick 2.0
    Rectangle {
        id: buttonId
        property alias label: labelTextId
        signal activated;
        border.width: 2
    
        Text {
            id: labelTextId
            anchors.centerIn: parent
        }
    
        MouseArea {
            id: mouseArea
            anchors.fill: parent
            onPressed: {
                buttonId.activated();
            }
        }
    }
    
    

    Thanks!

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jdcordoba
      wrote on 8 Jun 2016, 07:11 last edited by
      #2

      My dear friend, I'm afraid that QML models do not allow functions as data.

      Nevertheless, a workaround is not complicated:

      Main.qml:

      
      import QtQuick 2.0
      import QtQuick.Window 2.2
      Window {
          visible: true
          width: mb.width * 1.6
          height: mb.height * 1.6
          MyBtnRow {
              id: mb
              anchors.centerIn: parent
              btnData: [
                  {tag:"Jump", action:"elsewhere definded"},
                  {tag:"Run", action:"elsewhere definded"},
                  {tag:"Hide", action:"elsewhere definded"},
                  ]
              callbackList: {
                  "Jump": function(){console.log(" Jump Called OK!"); },
                  "Hide": function(){console.log(" Hide Called OK!"); },
                  "Run": function(){console.log(" Run Called OK!"); },
               }
          }
      }
      
      

      MyBtnRow.qml:

      import QtQuick 2.0
      
      Row {
          property var btnData
          property var callbackList  
          spacing: 3
          Repeater {
              id: rep
              model: btnData
              Btn {
                  id: btnId
                  width: 60;
                  height: 50;
                  label.text:  "Do " + modelData.tag
                  onActivated: {
                        try { callbackList[modelData.tag]() } catch (err) {console.log("  catch (err:" +err.message+") in Button "+label.text);};
                  }
              }
          }
      }
      
      
      

      Best regards

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jdcordoba
        wrote on 9 Jun 2016, 14:53 last edited by
        #3

        That's a brilliant and elegant solution!
        Thank you bro for spending your precious time with a newbie like me!

        1 Reply Last reply
        0
        • V Offline
          V Offline
          Vicky Sharma
          wrote on 10 Jun 2016, 14:51 last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0

          1/4

          7 Jun 2016, 13:12

          • Login

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