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. Call function from ancestor. How?
Qt 6.11 is out! See what's new in the release blog

Call function from ancestor. How?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 110 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.
  • B Offline
    B Offline
    bogong
    wrote last edited by
    #1

    Hello!

    Trying to create template QML object, something like this Template.qml:

    Rectangle {
    
    	id: oRoot;
    	color: "magenta";
    
    	MouseArea {
    
    		id: oMouseArea;
    		anchors.fill: parent;
    
    		onClicked: {
    
    			console.debug("Template OnClicked");
    
    			if ("mOnClicked" in parent) {
    				if (typeof parent.mOnClicked === "function") {
    					parent.mOnClicked();
    				} else {
    					console.debug("not a function");
    				}
    			} else {
    				console.debug("no method");
    			}
    		}		
    	}
    }
    

    This template should have mechanism to check if ancestor of this template has function, then call it. For now it's not working if there are created some component ComponentBasedOnTemplate.qml

    // Component
    Template {
    
        id: oRoot;
      
        function mOnClicked() {
    
            console.debug( "Component onClicked");
        }
    }
    

    How to make accessible ComponentBasedOnTemplate JS functions from Template?

    1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote last edited by
      #2

      The usual way to do this is to declare a signal in the root of your Template and then connect to it when using the type.

      So in Template.qml:

      Rectangle {
          id: oRoot
          signal clicked()
          // ...
          MouseArea {
              onClicked: oRoot.clicked()
              // ...
      

      And then in ComponentBasedOnTemplate.qml:

      Template {
          onClicked: console.debug( "Component onClicked")
      }
      

      I'll advise you to just use Button or another higher level type provided by Qt Quick Controls though.

      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