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 function through the QML ListModel

Pass function through the QML ListModel

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 2.5k 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.
  • D Offline
    D Offline
    devel
    wrote on last edited by devel
    #1
    import QtQuick 2.4
    import QtQuick.Controls 1.1
    
    Item {
    	Column {
    		TextArea {
    			id: txt
    			height: 20
    		}
    
    		Repeater {
    			model: ListModel {
    				Component.onCompleted: {
    					append({index: 0,  f: function() { txt.forceActiveFocus() }})
    				}
    			}
    
    			Component {
    				id: d
    
    				Button {
    					text: "btn"
    				}
    			}
    
    			delegate: Loader {
    				sourceComponent: d
    				onLoaded: {
    					item.onClicked.connect(f)
    				}
    			}
    		}
    	}
    }
    

    The button, generated from a ListModel, should be able to trigger an invokable that is specified in the model. How to do that?

    This example should get TextArea focused when the button is clicked. Instead, it gives: 'Error: Function.prototype.connect: target is not a function'.

    (In the real example, there is a C++ object with Q_INVOKABLE instead of TextArea. And the ListModel has a couple of additional fields and functions to pass.)

    p3c0P 1 Reply Last reply
    0
    • D devel
      import QtQuick 2.4
      import QtQuick.Controls 1.1
      
      Item {
      	Column {
      		TextArea {
      			id: txt
      			height: 20
      		}
      
      		Repeater {
      			model: ListModel {
      				Component.onCompleted: {
      					append({index: 0,  f: function() { txt.forceActiveFocus() }})
      				}
      			}
      
      			Component {
      				id: d
      
      				Button {
      					text: "btn"
      				}
      			}
      
      			delegate: Loader {
      				sourceComponent: d
      				onLoaded: {
      					item.onClicked.connect(f)
      				}
      			}
      		}
      	}
      }
      

      The button, generated from a ListModel, should be able to trigger an invokable that is specified in the model. How to do that?

      This example should get TextArea focused when the button is clicked. Instead, it gives: 'Error: Function.prototype.connect: target is not a function'.

      (In the real example, there is a C++ object with Q_INVOKABLE instead of TextArea. And the ListModel has a couple of additional fields and functions to pass.)

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi @devel,
      AFAIK ListElement roles cannot have scripts as its values.
      http://doc.qt.io/qt-5/qml-qtqml-models-listelement.html

      Values must be simple constants; either strings (quoted and optionally within a call to QT_TR_NOOP), boolean values (true, false), numbers, or enumeration values (such as AlignText.AlignHCenter).

      157

      D 1 Reply Last reply
      0
      • p3c0P p3c0

        Hi @devel,
        AFAIK ListElement roles cannot have scripts as its values.
        http://doc.qt.io/qt-5/qml-qtqml-models-listelement.html

        Values must be simple constants; either strings (quoted and optionally within a call to QT_TR_NOOP), boolean values (true, false), numbers, or enumeration values (such as AlignText.AlignHCenter).

        D Offline
        D Offline
        devel
        wrote on last edited by
        #3

        ListModel.append() was a workaround to overcome this limitation: at least the ids can be passed with it.

        I'm going to pass the Action objects there instead of plain functions for now.

        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