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. Pass callback function from model to view [ ListElement: cannot use script for property value]
Forum Updated to NodeBB v4.3 + New Features

Pass callback function from model to view [ ListElement: cannot use script for property value]

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 5 Posters 646 Views 3 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.
  • Y Offline
    Y Offline
    ynatynat
    wrote on 7 Feb 2023, 07:02 last edited by
    #1

    How to pass function from model to view?
    I wrote like below, but error happened with the message [ ListElement: cannot use script for property value].

        ListModel{
            id:quitModel
            ListElement{
                title:qsTr("back")
                func:stackView.pop();//<---------ERROR
            }
            ListElement{
    
                title:qsTr("quit")
                func:Qt.quit()
            }
        }
    
        GridView{
            model:quitModel
            anchors.fill: parent
    
            delegate:Button{
                width:cellWidth
                text:title
                onClicked:func
    
            }
    
        }
    
    D F R 3 Replies Last reply 9 Feb 2023, 17:26
    0
    • Y ynatynat
      7 Feb 2023, 07:02

      How to pass function from model to view?
      I wrote like below, but error happened with the message [ ListElement: cannot use script for property value].

          ListModel{
              id:quitModel
              ListElement{
                  title:qsTr("back")
                  func:stackView.pop();//<---------ERROR
              }
              ListElement{
      
                  title:qsTr("quit")
                  func:Qt.quit()
              }
          }
      
          GridView{
              model:quitModel
              anchors.fill: parent
      
              delegate:Button{
                  width:cellWidth
                  text:title
                  onClicked:func
      
              }
      
          }
      
      D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 9 Feb 2023, 17:26 last edited by
      #2

      @ynatynat

      May be try building the model dynamically & use it.
      See something like this.

      ListModel {
          Component.onCompleted: {
              [
                  {
                      name: "Dheeru",
                      fun: printMe()
                  },
                  {
                      name: "PthinkS.com",
                      fun: printAll()
                  }
              ].forEach(function(element1) { append(element1); });
          }
      }
      

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • Y ynatynat
        7 Feb 2023, 07:02

        How to pass function from model to view?
        I wrote like below, but error happened with the message [ ListElement: cannot use script for property value].

            ListModel{
                id:quitModel
                ListElement{
                    title:qsTr("back")
                    func:stackView.pop();//<---------ERROR
                }
                ListElement{
        
                    title:qsTr("quit")
                    func:Qt.quit()
                }
            }
        
            GridView{
                model:quitModel
                anchors.fill: parent
        
                delegate:Button{
                    width:cellWidth
                    text:title
                    onClicked:func
        
                }
        
            }
        
        F Offline
        F Offline
        fcarney
        wrote on 9 Feb 2023, 20:39 last edited by fcarney 2 Sept 2023, 20:40
        #3

        @ynatynat I have found the ListElement mangles some objects (Actions) as well. I ended up creating a list of dictionaries instead:

        property var quitModel: [
          { title: "back"; func: { stackView.pop() } },
          { title: "quit"; func: { Qt.quit() } },
        ]
        

        C++ is a perfectly valid school of magic.

        1 Reply Last reply
        0
        • Y ynatynat
          7 Feb 2023, 07:02

          How to pass function from model to view?
          I wrote like below, but error happened with the message [ ListElement: cannot use script for property value].

              ListModel{
                  id:quitModel
                  ListElement{
                      title:qsTr("back")
                      func:stackView.pop();//<---------ERROR
                  }
                  ListElement{
          
                      title:qsTr("quit")
                      func:Qt.quit()
                  }
              }
          
              GridView{
                  model:quitModel
                  anchors.fill: parent
          
                  delegate:Button{
                      width:cellWidth
                      text:title
                      onClicked:func
          
                  }
          
              }
          
          R Offline
          R Offline
          Rusticus
          wrote on 10 Feb 2023, 09:01 last edited by
          #4

          @ynatynat

          When you add like this, you add the result and not the function imho

          Try to add

          function () { Qt.quit() }
          
          1 Reply Last reply
          0
          • Y Offline
            Y Offline
            ynatynat
            wrote on 24 Jul 2023, 01:14 last edited by
            #5

            Sorry everyone, I gave up.

            1 Reply Last reply
            0
            • Y ynatynat has marked this topic as solved on 24 Jul 2023, 01:14
            • E Offline
              E Offline
              EricoDeMecha
              wrote on 8 Mar 2024, 16:25 last edited by
              #6

              Take a look at this and see if you can get something out of it.
              "Beginning with Qt 5.11 ListElement also allows assigning a function declaration to a role. This allows the definition of ListElements with callable actions."

              import QtQuick
              
              Window {
                  width: 640
                  height: 480
                  visible: true
                  title: qsTr("Hello World")
                  Rectangle {
                      anchors.fill: parent
              
                      ListModel {
                          id: fruitModel
                          property real appleValue: 2.45
                          property real orangeValue: 3.25
                          property real bananaValue: 1.95
                          ListElement {
                              name: "Apple"
                              // cost: 2.45
                              // cost: fluidModel.appleValue // Error: ListElement: cannot use script for property value
                              cost: function(newCost = 2.45){// value 2.45 could be stored in persistent settings
                                  return (fruitModel.appleValue = newCost);
                              }
                          }
                          ListElement {
                              name: "Orange"
                              // cost: 3.25
                              // cost: fluidModel.orangeValue // Error: ListElement: cannot use script for property value
                              cost: function(newCost = 3.25){
                                  return (fruitModel.orangeValue = newCost);
                              }
                          }
                          ListElement {
                              name: "Banana"
                              // cost: 1.95
                              // cost: fluidModel.bananaValue // Error: ListElement: cannot use script for property value
                              cost: function(newCost = 1.95){// value could stored in a settings variable
                                  return (fruitModel.bananaValue = newCost);
                              }
                          }
                      }
              
                      Component {
                          id: fruitDelegate
                          Row {
                              spacing: 10
                              Text { text: name }
                              // Text { text: '$' + cost } // if value
                              Text { text: '$' + cost() } // if function
                          }
                      }
              
                      ListView {
                          anchors.fill: parent
                          model: fruitModel
                          delegate: fruitDelegate
                      }
                  }
              }
              
              1 Reply Last reply
              0

              • Login

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