Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Update a QML CheckBox from a C++ function
Forum Updated to NodeBB v4.3 + New Features

Update a QML CheckBox from a C++ function

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.5k Views 2 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.
  • A Offline
    A Offline
    arealperson
    wrote on 13 Jun 2015, 17:03 last edited by arealperson
    #1

    Could some please help me, I'm really stuck, and its real important that I find out how to do this. Please.
    I have looked, but can't find the solution.

    I have a CheckBox and a Button Area defined inside my Main.qml file.
    This qml file gets loaded and set as the primary context from inside the main() function of the C++ program. Like this...

        QQuickView v;
        v.setSource(QUrl::fromLocalFile("main.qml"));
        v.show();
    

    Here's the Button Area... that is inside the Main.qml file.

            Rectangle {
                    width: 100
                    height: 50
                    color: "tomato"
                    Text {
                    anchors.fill: parent
                    text: "DoIt"
                }
                MouseArea {
                    anchors.fill: parent
                    onClicked: SomeClass.foo()
                }
            }
    

    ********************** Here is the Checkbox that the C++ needs to Check or unCheck ******************************
    This is what I need to set from the C++ code when I click the above button.

    This CheckBox is also located inside the Main.qml file

    CheckBox
        {
        id:chk
        anchors.bottom: parent.bottom
        anchors.margins: 10
        text: "Mirror"        
    
        Binding { target: chk; property: "checked"; value: FieldPtr.currentFieldPtr.map.Mirror.value }
    
        onClicked: FieldPtr.currentFieldPtr.map.Mirror.value = chk.checked
    }
    

    When I press the Button Area on the Main.qml page, a C++ function gets called.

    I need a C++ function call to cause the CheckBox to be checked when I press the Button Area. This needs to happen from the C++ code.

    My problem is, I don't know how to access the CheckBox control from the C++ code. I don't know how to do it.

    How do I do that ?

    Please help me if you can.
    And thank you for helping me.

    P 1 Reply Last reply 14 Jun 2015, 06:18
    0
    • A arealperson
      13 Jun 2015, 17:03

      Could some please help me, I'm really stuck, and its real important that I find out how to do this. Please.
      I have looked, but can't find the solution.

      I have a CheckBox and a Button Area defined inside my Main.qml file.
      This qml file gets loaded and set as the primary context from inside the main() function of the C++ program. Like this...

          QQuickView v;
          v.setSource(QUrl::fromLocalFile("main.qml"));
          v.show();
      

      Here's the Button Area... that is inside the Main.qml file.

              Rectangle {
                      width: 100
                      height: 50
                      color: "tomato"
                      Text {
                      anchors.fill: parent
                      text: "DoIt"
                  }
                  MouseArea {
                      anchors.fill: parent
                      onClicked: SomeClass.foo()
                  }
              }
      

      ********************** Here is the Checkbox that the C++ needs to Check or unCheck ******************************
      This is what I need to set from the C++ code when I click the above button.

      This CheckBox is also located inside the Main.qml file

      CheckBox
          {
          id:chk
          anchors.bottom: parent.bottom
          anchors.margins: 10
          text: "Mirror"        
      
          Binding { target: chk; property: "checked"; value: FieldPtr.currentFieldPtr.map.Mirror.value }
      
          onClicked: FieldPtr.currentFieldPtr.map.Mirror.value = chk.checked
      }
      

      When I press the Button Area on the Main.qml page, a C++ function gets called.

      I need a C++ function call to cause the CheckBox to be checked when I press the Button Area. This needs to happen from the C++ code.

      My problem is, I don't know how to access the CheckBox control from the C++ code. I don't know how to do it.

      How do I do that ?

      Please help me if you can.
      And thank you for helping me.

      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 14 Jun 2015, 06:18 last edited by
      #2

      @arealperson
      You will need a pointer to that CheckBox on c++ side. For that you can use findChild method. It works by finding the items by objectName. So provide an objectName for your CheckBox and then use findChild.
      Once you get that exact object you can set properties of it using setProperty. In this case you can set CheckBox's checked property to true or false from C++ side.

      157

      1 Reply Last reply
      0

      1/2

      13 Jun 2015, 17:03

      • Login

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