I need to know Button text length in pixels. How to do that ?
1 Answer
Look at the following code snippet:
import QtQuick 2.0
import Ubuntu.Components 0.1
MainView {
width: 160
height: 160
Button {
id: my_button
Text {
id: my_text
anchors {centerIn: parent }
text: my_button.width + " x " + my_button.height
}
Component.onCompleted: console.log(my_text.width)
}
}
The default text property of a button is just a string and will inherit the style applied on the Button component.
To get access to the string length in pixel, use instead a dedicated Text element inside your button so that you can access its pixel width like this: my_text.width
.
Running the above code with qmlscene logs 41.953125
on the standard output for the string "80 x 40"