Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-127297

Switching from pixelSize to pointSize is broken

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • 6.5.4
    • None

    Description

      How to properly switch from pixelSize to pointSize dynamically on a QML Text item? We have the functionality in QtDS that the user can pick the "size mode" for a text based item in QML. Meaning there is a ComboBox which shows "px" and "pt" and the user can select either of them. But this causes issues with the application not reacting properly to a change of that type due to outputting the following statement.

      Both point size and pixel size set. Using pixel size.

      I wrote a small application that mimics that behavior. I've checked qquickvaluetype.cpp setPointSize and setPixelSize which is where the message is coming from and it seems that setting one of the two to -1 should solve the issue, but it doesn't also undefined doesn't help.

      When changing to "pt" in the demo application and afterwards changing the the size value the text size doesn't change. Also from the start both values are set even though only pixelSize is set explicitly.

      import QtQuick
      import QtQuick.Controls
      
      Window {
          width: 640
          height: 480
          visible: true
          title: qsTr("Hello World")
      
          Column {
              anchors.centerIn: parent
              spacing: 20
      
              Rectangle {
                  anchors.horizontalCenter: parent.horizontalCenter
                  width: 400
                  height: 200
                  color: "transparent"
                  border.width: 1
                  border.color: "grey"
      
                  Text {
                      id: testText
                      text: "This is a test"
                      font.pixelSize: 12
                      //font.pointSize: -1
                      anchors.centerIn: parent
                  }
              }
      
              Grid {
                  anchors.horizontalCenter: parent.horizontalCenter
                  columns: 2
                  rows: 2
                  columnSpacing: 20
      
                  Text { text: "px"; font.bold: true }
                  Text { text: testText.font.pixelSize }
                  Text { text: "pt"; font.bold: true }
                  Text { text: testText.font.pointSize }
              }
      
              Row {
                  anchors.horizontalCenter: parent.horizontalCenter
                  spacing: 20
      
                  SpinBox {
                      id: sizeSpinBox
                      value: sizeType.currentText === "px" ? testText.font.pixelSize
                                                           : testText.font.pointSize
                      onValueModified: {
                          if (sizeType.currentText === "px") {
                              testText.font.pixelSize = sizeSpinBox.value
                          } else {
                              testText.font.pointSize = sizeSpinBox.value
                          }
                      }
                  }
      
                  ComboBox {
                      id: sizeType
                      model: ["px", "pt"]
                      onActivated: {
                          console.log("onActivated", sizeType.currentText)
      
                          if (sizeType.currentText === "px") {
                              let pt = testText.font.pointSize
                              testText.font.pointSize = -1//undefined
                              testText.font.pixelSize = Math.round(pt * 4/3)
                          } else {
                              let px = testText.font.pixelSize
                              testText.font.pixelSize = -1//undefined
                              testText.font.pointSize = Math.round(px * 3/4)
                          }
      
                          testText.forceLayout()
                      }
                  }
              }
          }
      }
      

      There is a discussion on teams about the topic.

      I've adapted my application from above to follow Fabians suggestion and it works. But this isn't a solution for QtDS. A pseudo solution for QtDS would be to write special code in the puppet to mimic the workaround proposed by Fabian in C++, but this would only solve the normal use case not taking into account state changes and the like.

      import QtQuick
      import QtQuick.Controls
      
      Window {
          width: 640
          height: 480
          visible: true
          title: qsTr("Hello World")
      
          Column {
              anchors.centerIn: parent
              spacing: 20
      
              Rectangle {
                  anchors.horizontalCenter: parent.horizontalCenter
                  width: 400
                  height: 200
                  color: "transparent"
                  border.width: 1
                  border.color: "grey"
      
                  Text {
                      id: testText
                      text: "This is a test"
                      font.pixelSize: 12
                      anchors.centerIn: parent
                  }
              }
      
              Grid {
                  anchors.horizontalCenter: parent.horizontalCenter
                  columns: 2
                  rows: 2
                  columnSpacing: 20
      
                  Text { text: "px"; font.bold: true }
                  Text { text: testText.font.pixelSize }
                  Text { text: "pt"; font.bold: true }
                  Text { text: testText.font.pointSize }
              }
      
              Row {
                  anchors.horizontalCenter: parent.horizontalCenter
                  spacing: 20
      
                  SpinBox {
                      id: sizeSpinBox
                      value: sizeType.currentText === "px" ? testText.font.pixelSize
                                                           : testText.font.pointSize
                      onValueModified: {
                          if (sizeType.currentText === "px") {
                              testText.font.pixelSize = sizeSpinBox.value
                          } else {
                              testText.font.pointSize = sizeSpinBox.value
                          }
                      }
                  }
      
                  ComboBox {
                      id: sizeType
                      model: ["px", "pt"]
                      onActivated: {
                          console.log("onActivated", sizeType.currentText)
      
                          if (sizeType.currentText === "px") {
                              let pt = testText.font.pointSize
      
                              let myFont = {}
                              Object.assign(myFont, testText.font)
                              delete myFont.pointSize
                              myFont.pixelSize = Math.round(pt * 4/3)
                              testText.font = myFont
                          } else {
                              let px = testText.font.pixelSize
      
                              let myFont = {}
                              Object.assign(myFont, testText.font)
                              delete myFont.pixelSize
                              myFont.pointSize = Math.round(px * 3/4)
                              testText.font = myFont
                          }
      
                          testText.forceLayout()
                      }
                  }
              }
          }
      }
      

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              qt.team.quick.subscriptions Qt Quick and Widgets Team
              henning Henning Gründl
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:

                Gerrit Reviews

                  There are no open Gerrit changes