I have a QML line edit, and am trying to use the input mask to ensure a valid UID is entered. My input mask is set like this:
inputMask: ">\\{NNNNNNNN-NNNN-NNNN-NNNN-NNNNNNNNNNNN\\}"
and my text property is set to: "{49f93de5-1da6-4e3a-a2e4-64795dc89ebb}"
When I run my program the curly braces are missing from the screen's edit field (but the hyphens are present and character masks are correctly applied). How can I show the { and } ? I assumed escaping them in the mask was all that was necessary.
I think your problem relates to this
Edited: wrapping the input string in qsTr() allows the curly braces to show in the input text.
in QML I use TextInput and it works :
import QtQuick 2.14
import QtQuick.Window 2.14
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
TextInput {
id: textInput
x: 116
y: 94
width: 237
height: 53
text: qsTr("{49f93de5-1da6-4e3a-a2e4-64795dc89ebb}")
font.pixelSize: 12
inputMask: ">\\{NNNNNNNN-NNNN-NNNN-NNNN-NNNNNNNNNNNN\\}"
}
}
Related
I have a strange behavior of DoubleValidator in TextInput. It allows to put both comma "," and point "." symbols when I type some float number. My current locale is "en_US" and apparently validator should restrict the possibility to type the comma symbol.
There is a simple piece of code where I can reproduce this behavior.
import QtQuick 2.9
import QtQuick.Window 2.3
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
TextInput {
id: inputField
anchors.centerIn: parent
validator: DoubleValidator { bottom: -Infinity;
top: Infinity;
decimals: 4;
notation: DoubleValidator.StandardNotation }
text: "0"
}
}
Later in code I'm trying to get number from this field
x = value ? Number.fromLocaleString(Qt.locale(), inputField.value)
And if I print the number with comma separator I reasonably get the warning
Warning: QML Error: Error: Locale: Number.fromLocaleString(): Invalid format
and x holds an integer part of float number.
I would appreciate if you can help me to understand what I'm doing wrong. My only purpose here is to correctly validate numbers with floating point depending on users locale.
I am writing a dialog box for a software plugin for Cura, a 3D printing slicer. When the user slices their file it brings up a dialog box to name the file before uploading it to the 3D printer. A python script generates the name in the format "print name - material-otherinformation.gcode" Right now, when the dialog loads, it highlights the whole text field except for the .gcode extension at the end. I would like it to only highlight a portion of that text field by default, namely the print name part. It is easy for me to return the length of that section as an integer and pass it to the QML file. I am definitely an amateur with QML, but it seems like the select function should be able to handle that but I can't figure out the usage. Any assistance or pointers would be much appreciated!
Here's a simplified version of the code. What I would like to do is add something to this so that just "word 1" is highlighted when the dialog box appears.
import QtQuick 2.1
import QtQuick.Controls 1.1
import QtQuick.Dialogs 1.2
import QtQuick.Window 2.1
import UM 1.1 as UM
UM.Dialog
{
id: base;
minimumWidth: screenScaleFactor * 400
minimumHeight: screenScaleFactor * 120
Column {
anchors.fill: parent;
TextField {
objectName: "nameField";
id: nameField;
width: parent.width;
text: "word1 - word2 - word3.gcode";
maximumLength: 100;
}
}
}
It's just a matter of when to use the TextInput::select() method. This could be in Component.onCompleted: of either the dialog or the text field, for example:
UM.Dialog
{
...
property int selectionLength: 0
Component.onCompleted: nameField.select(0, selectionLength);
...
TextField {
id: nameField;
text: "word1 - word2 - word3.gcode";
}
...
}
If selectionLength could change after dialog is created, then I'd create a separate function which can be called from different events or even directly:
UM.Dialog
{
...
property int selectionLength: 0
Component.onCompleted: select(selectionLength);
onSelectionLengthChanged: select(selectionLength);
function select(len) { nameField.select(0, len); }
...
TextField {
id: nameField;
text: "word1 - word2 - word3.gcode";
}
...
}
Obviously if the selection isn't going to be from the first character then some adjustment to this strategy will be needed.
The following Qml code gives the following output (expected):
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Window 2.11
Window {
height: 200
width: 200
visible: true
Button {
id: root
text: "Text"
anchors.centerIn: parent
Item {
anchors.fill: parent
Text {
text: "Item.Text"
color: "red"
}
}
}
}
The following code (using contentItem) produces a different output:
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Window 2.11
Window {
height: 200
width: 200
visible: true
Button {
id: root
text: "Text"
anchors.centerIn: parent
contentItem: Item {
anchors.fill: parent
Text {
text: "Item.Text"
color: "red"
}
}
}
}
The Qt documentation is not very clear, at least for me. The question is what does the property contentItem do? When it should be used?
Short answer: The contentItem is meant for customizing the control and replaces the existing implementation of the visual foreground element by your text.
Long answer:
A Quick Item has a so called "default property" - the data property. By definition, if you add an item as child of another item, it is instead assigned to the default property. Which means the following example:
Item {
Text { text: "test1"}
}
Is actually identical to:
Item {
data: [
Text { text: "test2"}
]
}
If you know look at your example, in the first variant, you simply append a child item to the root button. Since no further information is given, it is placed at the coordinates (0,0) within it's parent.
The contentItem property however is defined in the documentation as follows:
This property holds the visual content item.
In case of a Button it is an internally used Label to display the text property of the button. It exists to modify the appereance of the button.
In your second example, you "customize" the button by replacing the internal label with your custom Text - but without any code to properly position or fill the item. The correct way to declare a content item can be found in the customization guide:
Button {
id: control
text: qsTr("Button")
contentItem: Text {
text: control.text
font: control.font
opacity: enabled ? 1.0 : 0.3
color: control.down ? "#17a81a" : "#21be2b"
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight
}
// ...
}
You could either define it as part of a custom style, or create a MyButton.qml where do exactly this and can then use MyButton in other QML files, giving you a custom styled button whilest keeping the API intact (like beeing able to set the text via the text property etc.)
I hope this was sufficient enough to help you understand how it works.
Doing my first steps in QML, so this might be obvious...
When using a TextInput with a simple validator (say an IntValidator), is there a way to know the maximum width that TextInput field will take?
As an example, if I create a IntValidator for a number from 0 to 999, I would like to find the width required to display that 999 (or whichever will be the widest, based on the font etc...).
I am trying to wrap that textinput into an item which will have a fixed size, just the right size for the worst case input, nothing less, nothing more?
Thanks.
Use TextMetrics:
import QtQuick 2.9
import QtQuick.Controls 2.2
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
TextMetrics {
id: textMetrics
font: textField.font
text: "999"
}
TextField {
id: textField
width: textMetrics.width + leftPadding + rightPadding
validator: IntValidator {
bottom: 0
top: 999
}
}
}
I am building a simple QML application that makes use of the ArcGIS SDK, the purpose right now is just to learn the SDK features.
What I'd like to do is have a zoomable map and a text box that displays the current map scale. Below is the code I have written for this, based upon the samples on the ArcGIS website.
import QtQuick 2.6
import QtQuick.Controls 1.4
import Esri.ArcGISRuntime 100.1
ApplicationWindow {
id: appWindow
width: 800
height: 600
title: "Untitled"
MapView {
id: mainmapview
attributionTextVisible: false
anchors.topMargin: 0
anchors.rightMargin: 0
anchors.fill: parent
focus: true
Map {
id: mainmap
BasemapLightGrayCanvasVector {}
}
onMapScaleChanged: scaletext.text=mainmapview.scale.toString()
Text {
id: scaletext
x: 10
y: 10
width: 285
height: 45
text: qsTr("Text")
font.pixelSize: 12
}
}
}
The map loads and I can see it OK, but the scale textbox doesn't work: it always shows the scale as '1', no matter how much I zoom in or out. Obviously this isn't correct. Am I messing up the type conversion to text?
Any pointers as to how to resolve this would be great. Thanks.
Actually I figured it out myself. This line:
onMapScaleChanged: scaletext.text=mainmapview.scale.toString()
Should be:
onMapScaleChanged: scaletext.text=mainmapview.mapScale.toString()
Rather than delete the question I've answered it in case anyone else has the same problem and can find this through google.