i have a custom keyboard in qml and has a textField for it so i can use that value somewhere else. When qml page is initialized in application, textfield needs to be clicked to type inside. What i want is when qml initialized should be able to push buttons without clicking textfield first. Here is what i am using
TextField {
id:inputValueArea
width:200;height:35
anchors.fill : parent
background:
font.pixelSize: 20
placeholderText: ""
color: "#FFFFFF"
echoMode: TextInput.Normal
onAccepted: focus = false
Keys.onEscapePressed: undo()
}
I tried to use activeFocusOnPress and inputMethodComposing but it did not work.
(source: https://doc.qt.io/qt-5/qml-qtquick-controls-textfield.html#activeFocusOnPress-prop)
What can i code for that? Since i have keyboard as a seperated qml, i couldn't use this solution as a solution (Qt QML Focus an Item (TextField) when showing a StackView Page)
Related
I am using Repeater and Delegate to display a list, it all works fine but when i clean the model the GUI does not clean it, still old list is shown. I pass the model from cpp connector to qml.
I have tried update(), repaint() but no avail.
Any leads would be helpful.
Cpp code:
void ModelCpp::clearMessage() {
qDeleteAll(messageList);
messageList.clear();
emit messageListUpdated();
}
QML
ColumnLayout {
spacing: 1
Repeater{
model: modelCpp.messageList
delegate: Rectangle {
visible: true
color: colorBoxShader
Layout.preferredWidth: 100
Layout.preferredHeight: 20
RowLayout{
anchors.fill: parent
Text {
text: modelData.label
Layout.alignment: Qt.AlignLeft
Layout.leftMargin: layoutLeftMargin
color: "white"
font.bold: true
...
```
Edit:
I narrowed down to QStackedWidget, I am using QStackedWidget to load different pages. It was clear on one screen but now I noticed it is happening on all screen. Until the qml page is not current page, it is not updated and has old value. How can I rerender page in background when it is not current page.
I figured out the issue. With QStackedWidget only the visible page is updated/repainted but not the invisible page. When the invisible page becomes visible it is painted and you will see old values briefly before new values are painted. Create new QQuickWidget each time and add it to QStackedWidget.
I want to have a widget in QML which has combination of the following behaviors:
1) Multi line edit
2) Auto scroll the content as and when I hit newline. (The content on top of the page keeps going up as and when I enter new content at the bottom)
3) Have a placeholder text functionality.
As far as I know, only Text and TextField are having placeholder text property and only TextArea is having a multi line edit plus auto scroll. But If there is any such widget with all the combinations then, my question “Dynamically swap the visibility/opacity between overlapping Text and TextArea “ would be invalid.
In case there is no such widget (I wonder why), I am thinking to have a rectangle which has both Text and TextArea overlapping and based on the below logic I want to have the visibility/opacity/focus on one of them:
If the Text Area is empty (0 characters), then have the Text in the foreground with focus and with the placeholder text “Enter some text”. But as soon as the user starts typing, the Text should lose the focus, opacity and go to background and the TextArea should gain the focus and come to the foreground and start accepting multi line input. Similarly, when TextArea is in the foreground and is empty (0 characters) and when the user click on any other widget outside my Rectangle, the Text should again gain the focus, come to the foreground and display the placeholder again.
I tried to get inspiration from this code, but failed miserably, it would be helpful if anyone can help me with a few lines of code or give me some pointers on how to solve this.
You can implement placeholderText for TextArea the same way Qt does in TextField. The source can be found here: TextField.qml
When you remove all the comments and properties, you basically have a background and on top of that a MouseArea, the placeholderText Text and a TextInput. Since you need to have the placeholder visually below the TextArea, you must have a transparent background:
PlaceholderTextArea.qml
import QtQuick 2.3
import QtQuick.Controls 1.2
Rectangle {
property alias placeholderText: placeholder.text
id: background
width: 640
height: 480
color: "#c0c0c0"
Text {
id: placeholder
anchors.fill: parent
renderType: Text.NativeRendering
opacity: !textArea.text.length && !textArea.activeFocus ? 1 : 0
}
TextArea {
id: textArea
anchors.fill: parent
backgroundVisible: false
}
}
and use your component:
PlaceholderTextArea {
placeholderText: qsTr("Hello World")
anchors.fill: parent
}
Here's an alternative implementation, that works a bit better for me:
import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
Item
{
property alias placeholderText: placeholder.text
property bool __shouldShowPlaceholderText:
!textArea.text.length && !textArea.activeFocus
// This only exists to get at the default TextFieldStyle.placeholderTextColor
// ...maybe there is a better way?
TextField
{
visible: false
style: TextFieldStyle
{
Component.onCompleted: placeholder.textColor = placeholderTextColor
}
}
TextArea
{
id: placeholder
anchors.fill: parent
visible: __shouldShowPlaceholderText
activeFocusOnTab: false
}
TextArea
{
id: textArea
anchors.fill: parent
backgroundVisible: !__shouldShowPlaceholderText
}
}
I'm writing a test application to try out what QML has to offer. I have created a simple button and tried to create a tooltip on mouse hover event. I found several solutions already how to make that happen (example) and it is not a problem.
In the documentation, however, I have encountered a button property called tooltip. Now I assumed if a built-in component has such a property then creation of tooltip is automated. Apparently that is not the case, since defining tooltip property did not change anything.
The question is what is this property actually used for?
Button {
id: myButton
x: 10
y: 10
text: "Click me"
tooltip: "Some tooltip"
}
Showing of tooltip requires receiving of mouse hover events and this is possible only if your button is not overlapped with some another MouseArea with hoverEnabled property equal to true. Following example shows tooltip fine on OS X and Qt 5.2.1:
import QtQuick 2.0
import QtQuick.Controls 1.1
Rectangle {
width: 360
height: 360
Text {
anchors.centerIn: parent
text: "Hello World"
}
Button {
id: myButton
x: 10
y: 10
text: "Click me"
tooltip: "Some tooltip"
}
}
SDK: Qt Creator 2.4.1
Target: Nokia N9 and Windows 7
If I do the following in a qml file
import QtQuick 1.1
import com.nokia.meego 1.0
Page {
id: myShowChangeLogPage
TextEdit {
id: changeLogArea
anchors.top: titleBackground.bottom
width: parent.width
height: 300
text: "1\n1\n1\n1\n2\n1\n1\n1\n1\n1\n3\n1\n1\n1\n4\n1\n1\n1\n1\n5\n1\n1\n1\n6\n1\n1\n1\n7\n1\n1\n1\n8\n\n\n\n\n9"
font.pixelSize: 20
textFormat: TextEdit.AutoText
readOnly: true
wrapMode: TextEdit.WordWrap
}
}
The TextEdit area do not behave as I expected.
The String will be printed outside the size of the TextEdit area, that is,
it continues beneath the bottom screen edge.
There is no scrollbar/slider to the right
I was expecting that the the TextEdit element should automatically create a
scrollbar/slider if the string is to large to fit within the boundaries.
I have been told that TextEdit should do this and there is no need for a Flicker
or ScrollArea.
I have tried other type of components such as Text and TextEdit and also encapsulate
the TextEdit in a rectangle without any luck.
Regards
I read this right at the beginning of the documentation regarding the TextEdit element:
Note that the TextEdit does not implement scrolling, following the
cursor, or other behaviors specific to a look-and-feel.
There is also a complete example of how to implement scrolling for following the cursor.
TextEdit in qml doesn't implement scrolling. You must to use another one, such as TextArea (https://doc.qt.io/qt-5/qml-qtquick-controls2-textarea.html), then place it inside a ScrollView. Here's an example:
// Creating a scrollable TextEdit
ScrollView {
id: view
anchors.fill: parent
TextArea {
text: "TextArea\n...\n...\n...\n...\n...\n...\n"
}
}
I have a QML TextField in Qt creator. I want to get the text from the textfield UI.
How do I do that?
You need to have 'named' your TextFields with ids. If an object has an id, you can use it as a handle to access properties from it. For example, imagine you want to clone the text from one textfield to another:
TextField {
id: textField1
text: "Text"
}
TextField {
id: textField2
text: textField1.text
}
You can find more detail here.