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 how to center 4 labels in a rectangle
Forum Updated to NodeBB v4.3 + New Features

QML how to center 4 labels in a rectangle

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 4 Posters 562 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.
  • A Offline
    A Offline
    astoffregen
    wrote on last edited by
    #1

    I am trying to center 4 symbols in a rectangle starting from the center.

    The rectangle code looks (simplified) like this

    Rectangle { id: userlogon_passwords; Layout.fillWidth: true; Layout.fillHeight: true; Layout.preferredHeight: 250; color: "yellow"
                RowLayout { anchors.centerIn: parent 
                    spacing: 80
                    UserPasswordField { index: 0; p_isSelected: userlogon.p_selectedField == 0 }
                    UserPasswordField { index: 1; p_isSelected: userlogon.p_selectedField == 1 }
                    UserPasswordField { index: 2; p_isSelected: userlogon.p_selectedField == 2 }
                    UserPasswordField { index: 3; p_isSelected: userlogon.p_selectedField == 3 }
                }
            }
    

    UserPasswordField.qml looks like this

    Item {
        id: root
        property int index
        property bool p_isSelected
        property bool p_isTimerRunning
        property bool p_isRotated: p_isSelected && p_isTimerRunning 
        property int p_value    
    
        Label {
            font.pixelSize: root.p_isRotated ? userlogon_passwords.height * 0.5 : userlogon_passwords.height * 0.4
            font.bold: root.p_isSelected
            color: configuration.getColor(root.p_isSelected ? Colors.PasswordEnabled : Colors.PasswordDisabled)
            text: root.p_isRotated ? root.p_value : "*"
            style: Text.Outline
            styleColor: "darkgray"
        }
    }
    

    Result is this

    stars.png

    The stars are shifted to the right, but should move further to the left. Is there a simple trick?

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

      remove the outer Item in UserPasswordField.qml

      A 1 Reply Last reply
      1
      • JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by
        #2

        https://forum.qt.io/topic/141934/calculate-width-of-qml-item/4

        1 Reply Last reply
        0
        • F Offline
          F Offline
          freedbrt
          wrote on last edited by
          #3

          You didnt set PasswordTextField width and height

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

            remove the outer Item in UserPasswordField.qml

            A 1 Reply Last reply
            1
            • GrecKoG GrecKo

              remove the outer Item in UserPasswordField.qml

              A Offline
              A Offline
              astoffregen
              wrote on last edited by
              #5

              @GrecKo Sorry for late response. It works. Can you describe where the problem with the outer item is?

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

                You Item had a width and height of 0.
                Label has implicit width and height based on its content.

                1 Reply Last reply
                1

                • Login

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