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. [Solved] How to make dynamically created "Rectangle" components to bold one "Rectangle" text and unbold another when clicked?
Forum Updated to NodeBB v4.3 + New Features

[Solved] How to make dynamically created "Rectangle" components to bold one "Rectangle" text and unbold another when clicked?

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

    Hi,
    I want to do when I clicked dynamically created "rectangle" component, that component text goes bold, and then I clicked another component, previous clicked component goes unbold, i tryied different ways to do that, but nothing work.

    Here is my "Rectangle" component:

    @import QtQuick 2.0

    Rectangle {
    id: btn;
    width: 100;
    height: 100;
    color: "lightblue";

    property string btn_text: "";
    property string txt_bold;
    
    Text {
        id: txt;
        anchors.centerIn: parent.Center;
        color: "black";
        text: btn.btn_text;
        font.bold: btn.txt_bold;
    }
    
    MouseArea {
        id: mouse;
        anchors.fill: parent;
        onClicked: {
            btn.txt_bold = true;
            console.log("Clicked " + txt.text );
        }
    }
    

    }
    @

    And my main where i create dynamically components:

    @import QtQuick 2.0

    Rectangle {
    id: test;
    width: Style.width;
    height: Style.height;
    color: "#ccc";

    Component.onCompleted: {
        for(var i = 0; i<5; i++) {
            var comp = Qt.createComponent("test_comp.qml");
            var object = comp.createObject(test, {"x":50,"y":(10 * i)*i});
            object.btn_text = "Button " + i;
            object.y = (object.width + 10) * i;
        }
    }
    

    }
    @

    Any idea ? :)

    1 Reply Last reply
    0
    • jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #2

      Track the currently selected child Rectangle using a property in the parent. Have the onClicked handler reset the bold flag of the selected rectangle, and then update the tracking property.

      Given that font.bold is boolean, txt_bold should be boolean rather than a string. Instead of having properties that only serve to provide an [incorrectly typed] external interface, consider using aliases.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      0
      • G Offline
        G Offline
        gedixss
        wrote on last edited by
        #3

        Could you write an example, please ?

        1 Reply Last reply
        0
        • G Offline
          G Offline
          gedixss
          wrote on last edited by
          #4

          Solution :

          @import QtQuick 2.0

          Rectangle {
          id: test;
          width: Style.width;
          height: Style.height;
          color: "#ccc";

          property Rectangle curr_track;
          
          Component.onCompleted: {
              for(var i = 0; i<5; i++) {
                  var comp = Qt.createComponent("test_comp.qml");
                  var object = comp.createObject(test, {"x":50,"y":(10 * i)*i});
                  object.btn_text = "Button " + i;
                  object.y = (object.width + 10) * i;
                  curr_track = object;
              }
          }
          

          }
          @

          @import QtQuick 2.0

          Rectangle {
          id: btn;
          width: 100;
          height: 100;
          color: "lightblue";

          property string btn_text: "";
          property bool txt_bold;
          
          Text {
              id: txt;
              anchors.centerIn: parent.Center;
              color: "black";
              text: btn.btn_text;
              font.bold: btn.txt_bold;
          }
          
          MouseArea {
              id: mouse;
              anchors.fill: parent;
              onClicked: {
                  curr_track.txt_bold = false;
                  btn.txt_bold = true;
                  curr_track = btn;
              }
          }
          

          }
          @

          1 Reply Last reply
          0
          • p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by
            #5

            Hi,

            If you consider using inbuilt elements such as Columns,Repeater and Bindings you can eliminate some of the javascript code.
            Following is the modification of your code which does the same thing:

            main.qml
            @
            import QtQuick 2.0

            Rectangle {

            id: test;
            width: 600;
            height: 600;
            color: "#ccc";
            
            property int currentIndex
            
            Column {
                anchors.centerIn: parent
                spacing: 10
                Repeater {
                    model:5
                    TestComp {
                        btn_text: "Button" + index
                    }
                }
            }
            

            }
            @

            TestComp.qml
            @
            import QtQuick 2.0

            Rectangle {
            id: btn;
            width: 100;
            height: 100;
            color: "lightblue";

            property string btn_text
            
            Text {
                id: txt;
                anchors.centerIn: parent;
                color: "black";
                text: btn.btn_text;
                font.bold: currentIndex===index
            }
            
            MouseArea {
                id: mouse;
                anchors.fill: parent;
                onClicked: currentIndex = index
            }
            

            }
            @

            P.S: Your code is also correct and valid.

            157

            1 Reply Last reply
            0
            • G Offline
              G Offline
              gedixss
              wrote on last edited by
              #6

              Thanks ;))

              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