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. Set onClicked function based upon state
Forum Updated to NodeBB v4.3 + New Features

Set onClicked function based upon state

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 3 Posters 1.1k 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.
  • L Offline
    L Offline
    logic_cube
    wrote on last edited by
    #1

    What I would like is that if the object is in a certain state it should call func1 when onClicked is called and in a different state should call a different function. So instead of something like this:

    @Rectangle {
    anchors.fill: parent
    anchors.margins: 20

    MouseArea {
    id: area
    anchors.fill: parent
    onClicked:{
    if state == "one":
    ... do somethone for one...
    else:
    ... do somethone for two...
    }
    }
    }@

    I want something like this.

    @states [
    State {
    name = "one"
    "somehow set area.onclicked to handleOne"
    },
    State {
    name = "two"
    "somehow set area.onclicked to handleTwo"
    },
    ]

    function handleOne() {
    }

    function handleTwo() {
    }

    Rectangle {
    anchors.fill: parent
    anchors.margins: 20

    MouseArea {
    id: area
    anchors.fill: parent
    }
    }@

    1 Reply Last reply
    0
    • C Offline
      C Offline
      chrisadams
      wrote on last edited by
      #2

      There are various ways to do this. The first is to use a control variable and "trampoline" a function call in the onClicked handler.

      eg: onClicked: if (shouldCallFunctionOne) functionOne(); else functionTwo();

      The second is to use "var" properties and store a reference to the function you wish to call in it - but this is a Qt5-only solution.

      Cheers,
      Chris.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Roland_R
        wrote on last edited by
        #3

        You should be able to modify the signal handler from JS like this:

        @
        State {
        name: "one"
        StateChangeScript {
        script: {
        area.clicked.disconnect( handleOne );
        area.clicked.connect( handleTwo );
        }
        }
        @

        But note: if you add a handler to clicked while this event is currently handled, the newly added handler will be called immediatly.

        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