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
Forum Updated to NodeBB v4.3 + New Features

Pass function through the QML ListModel

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 2.5k 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.
  • D Offline
    D Offline
    devel
    wrote on 25 May 2016, 15:49 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.)

    P 1 Reply Last reply 26 May 2016, 06:37
    0
    • D devel
      25 May 2016, 15:49
      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.)

      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 26 May 2016, 06:37 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 26 May 2016, 06:56
      0
      • P p3c0
        26 May 2016, 06:37

        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 26 May 2016, 06:56 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

        1/3

        25 May 2016, 15:49

        • Login

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