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. QML proper using nested functions
Forum Updated to NodeBB v4.3 + New Features

QML proper using nested functions

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmlfunctionnestedstylebutton
2 Posts 2 Posters 668 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.
  • K Offline
    K Offline
    Kyeiv
    wrote on 27 Jan 2020, 07:35 last edited by
    #1

    Hi all!
    My code looks more or less like this:

    someJSscriptFile.js
    
    function getCurrentColor()
    {
    	return __getColor(control.currentValue)
    }
    
    function __getColor(value)
    {
    	if (control.pressed)
    	{
    		return "grey"
    	}
    
    	if(control.activated === false)
    	{
    		return "white"
    	}
    
        if(value ===0)
        {
            return "red";
        }
        return "green";
    }
    
    
    item1.qml
    
    item
    {
    id: defItem
      property var color : getColor()
    Text
    {
     text: "sometext"
    color: defItem.color
    }
    
    function getColor()
     {
         return someJSscriptFile.getCurrentColor()
     {
    }
    
    item2.qml
    
    ButtonStyle
    {
      property var color2 : getColor2()
    
    function getColor2()
     {
         return myItem.getCurrentColor()
     {
    
    property Component myItem: item1
    {
      color: color2
    }
    
    onSomeSignal()
    {
     label = myItem
    }
    }
    
    someLayout.qml
    RowLayout
    {
     Button
    {
      style:item2{}
    }
    }
    
    

    So my problem is that the color of text is black and I don't know how to fix it, am I missing some bindings or get the whole concept of QML wrong?

    Cheers, Kris

    1 Reply Last reply
    0
    • R Offline
      R Offline
      rrlopez
      wrote on 29 Jan 2020, 15:50 last edited by
      #2

      Hi @Kyeiv, I just did a quick test application and it worked for me:

      main.qml

      import QtQuick 2.9
      import QtQuick.Window 2.2
      
      import "someJSscriptFile.js" as Jss
      
      Window {
          visible: true
          width: 640
          height: 480
          title: qsTr("Hello World")
      
          Rectangle {
              id: rect
              color: Jss.getCurrentColor()
              anchors.fill: parent
          }
      }
      

      someJSScriptFile.js

      function getCurrentColor()
      {
          return __getColor(rect.currentValue)
      }
      
      function __getColor(value)
      {
          if (rect.pressed)
          {
              return "grey"
          }
      
          if(rect.activated === false)
          {
              return "white"
          }
      
          if(value ===0)
          {
              return "red";
          }
          return "green";
      }
      

      Is the variable "control" defined in your QML? Or what are you trying to use it for?

      Lic-Ing. Rodrigo Lopez Gonzalez
      Embedded Software Engineer
      RidgeRun Engineering Ltd.
      www.ridgerun.com
      Email: rodrigo.lopez@ridgerun.com

      1 Reply Last reply
      0

      2/2

      29 Jan 2020, 15:50

      • Login

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