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. Trying to use FontMetrics in own Item
Forum Updated to NodeBB v4.3 + New Features

Trying to use FontMetrics in own Item

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
7 Posts 2 Posters 1.2k Views
  • 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.
  • F Offline
    F Offline
    Farling
    wrote on last edited by
    #1

    I have the following basic code in MyField.qml

    TextInput {
       FontMetrics {
          id: fm
          font: parent.font
      }
      width: maximumLength * fm.averageCharacterWidth + 4
      height: fm.height + 4
    }
    

    And in another file, I use this object, e.g.

    Row {
       MyField {
          text: "Hello"
          font.bold: true
       }
       MyField {
          text:"World"
       }
    

    But I always get the error "Unable to assign [undefined] to QFont"

    How do I set the font within the FontMetrics to be the same font that has been selected for use by the user of MyField?

    F 1 Reply Last reply
    0
    • speqtrS Offline
      speqtrS Offline
      speqtr
      wrote on last edited by
      #2

      Try to use aliases:

      TextInput {
          property alias font: fm.font
      
          FontMetrics {
              id: fm
          }
      
          width: maximumLength * fm.averageCharacterWidth + 4
          height: fm.height + 4
      }
      
      F 1 Reply Last reply
      0
      • speqtrS speqtr

        Try to use aliases:

        TextInput {
            property alias font: fm.font
        
            FontMetrics {
                id: fm
            }
        
            width: maximumLength * fm.averageCharacterWidth + 4
            height: fm.height + 4
        }
        
        F Offline
        F Offline
        Farling
        wrote on last edited by
        #3

        @speqtr Thanks, this gets me closer to the solution. However setting font.pixelSize in an instance of MyField in the Row sets the correct field size, but the TextInput doesn't display the correct font (due to the alias?).

        1 Reply Last reply
        0
        • F Farling

          I have the following basic code in MyField.qml

          TextInput {
             FontMetrics {
                id: fm
                font: parent.font
            }
            width: maximumLength * fm.averageCharacterWidth + 4
            height: fm.height + 4
          }
          

          And in another file, I use this object, e.g.

          Row {
             MyField {
                text: "Hello"
                font.bold: true
             }
             MyField {
                text:"World"
             }
          

          But I always get the error "Unable to assign [undefined] to QFont"

          How do I set the font within the FontMetrics to be the same font that has been selected for use by the user of MyField?

          F Offline
          F Offline
          Farling
          wrote on last edited by Farling
          #4

          After some more testing, I figured it out (I'm new to QML). I should have been using the onCompleted event to set the size after the font is fully initialised (in my particular case I don't want the width/height to be overridden).

          TextInput {
              FontMetrics {
                  id: fm
              }
          
              maximumLength: 10
              selectByMouse: true
              Component.onCompleted: {
                  fm.font = font
                  width = (maximumLength + 1) * fm.averageCharacterWidth
                  height = fm.height + 4
              }
          }
          
          1 Reply Last reply
          0
          • speqtrS Offline
            speqtrS Offline
            speqtr
            wrote on last edited by
            #5
            This post is deleted!
            F 1 Reply Last reply
            0
            • speqtrS speqtr

              This post is deleted!

              F Offline
              F Offline
              Farling
              wrote on last edited by
              #6

              @speqtr When I said "TextInput doesn't display the correct font" I meant that the font used for the field was ignoring the font.pixelSize which was being set, although the spacing between the fields (based on the width/height) was correct.

              1 Reply Last reply
              0
              • speqtrS Offline
                speqtrS Offline
                speqtr
                wrote on last edited by
                #7

                Sorry for misunderstanding.

                Can you try this one as a MyField?

                    TextInput {
                        id: myField
                        width: (maximumLength + 1) * fm.averageCharacterWidth
                        height: fm.height + 4
                        maximumLength: 10
                        selectByMouse: true
                        clip: true
                
                        FontMetrics {
                            id: fm
                            font: myField.font
                        }
                    }
                

                In this case setting font.pixelSize should work perfectly fine.

                MyField {
                    font.pixelSize: 14
                }
                

                Sorry for misleading you in a first place.

                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