Detect changes on every character typed in a TextField QML - qml

I want to detect whenever the value in a TextField is altered.
All I see is the editingFinished() and textEdited() signals which
emits only when the space character,backspace or enter key is pressed.
Tried Keys.onPressed{} in the TextField component but didn't work
as expected
Anyone with a better idea to do this?

It might be an issue with text not being committed yet to the field. This happens especially on Android devices.
In that case I suggest to test these two solutions:
listen on displayText property changes with onDisplayTextChanged slot, or
listen on preeditText property changes with onPreeditTextChanged
If this is the case and you would like the text be committed after each character, you could do something like:
TextField {
...
onPreeditTextChanged: if ( this is android ) Qt.inputMethod.commit() // to avoid Android's uncommited text
}
Make sure you do this only on Android (other platforms might double the characters on some keyboards)! It is a little hack. I would first try the displayText property.

So after a few hours of trying, I think I found a way which works at least for me using the contentSizeChanged() signal
TextField {
id: nameValue
anchors{
top: parent.top
left: parent.left
right: parent.right
margins: Style.margin*2
}
placeholderText: Style.selectedName
Material.accent: Style.colorAccent
font.pixelSize: Style.smallTinyFontSize
verticalAlignment: Qt.AlignVCenter
onContentSizeChanged: console.log("contentSizeChanged")
onTextChanged: console.log("TextChanged ")
onTextEdited: console.log("TextEdited")
onEditingFinished: console.log("EditingFinished")
}
Hello World typed into the textField with Enter key pressed produces this output at the console

Related

QML (KDE Plasmoid): Can't get seamless scrolling text to loop accordingly

I'm working on a plasmoid for KDE Plasma and can't figure out how to implement a scrolling header bar, similar to ones you see at the bottom of the screen on news channels. I want to have a line of text which horizontally scrolls inside a colored box at a fixed speed, looping forever and seamlessly starting over once it reaches the end. I figured out the basic part of the looping transition, allowing the text to move outside of its box then come out the other end:
Rectangle {
width: myItem.width
height: myItem.height
color: myColor
Text {
width: myItem.width
wrapMode: "NoWrap"
maximumLineCount: 1
color: theme.textColor
text: "Whatever goes here."
SequentialAnimation on x {
running: true
loops: Animation.Infinite
NumberAnimation { from: -myItem.width; to: myItem.width; duration: 1000; easing.type: Easing.InOutQuad }
PauseAnimation { duration: 250 }
}
}
}
But this doesn't take into account the length of the text string in order to adjust the start / end position and duration to its real width. It also requires moving the text completely out of bounds then back in, leaving the box empty for one frame... I wonder if there's a way to make it connect seamlessly. It also doesn't notice when I resize the plasmoid and adapt the animation range, only the scale detected at the start is taken into account. How do you suggest redoing that definition to work around scale issues and get consistent results with any box size and text length?

Qt QML Settings.hasTouchScreen returns false

I am trying to find out why flicking is not working with a TreeView example on my Raspberry Pi3 with touch screen.
Looking at the qml code of TreeView.qml, e.g.
https://github.com/RSATom/Qt/blob/master/qtquickcontrols/src/controls/TreeView.qml:
BasicTableView {
...
__mouseArea: MouseArea {
id: mouseArea
parent: __listView
width: __listView.width
height: __listView.height
z: -1
propagateComposedEvents: true
focus: true
// If there is not a touchscreen, keep the flickable from eating our mouse drags.
// If there is a touchscreen, flicking is possible, but selection can be done only by tapping, not by dragging.
preventStealing: !Settings.hasTouchScreen
...
}
}
By similarly looking at the qml code for BasicTableView.qml, it seems that behavior is controlled by Settings.hasTouchScreen.
According to:
https://code.woboq.org/qt5/qtquickcontrols/src/controls/Private/qquickcontrolsettings.cpp.html
it corresponds to the following method:
bool QQuickControlSettings1::hasTouchScreen() const
{
const auto devices = QTouchDevice::devices();
for (const QTouchDevice *dev : devices)
if (dev->type() == QTouchDevice::TouchScreen)
return true;
return false;
}
However, in my case, Settings.hasTouchScreen returns false; i.e. the touch screen (although working for the rest), is not
correctly detected by the QML environment, which probably explains why the flicking does not work.
According to https://doc.qt.io/qt-5/qtouchdevice.html, my touch device should have been registered somehow by the private QWindowSystemInterface::registerTouchDevice() method, but wasn't.
How can I get this to work?
Thanks!
It seems not to work correctly with tslib, but works by using the evdevtouch plugin which is enabled by adding the following command line arguments when launching the program:
-plugin evdevtouch:/dev/input/eventX
where eventX is the event assigned to the touch input.
With that, QTouchDevice::devices() is no longer empty, and the flicking of the TreeView works.

MouseArea in a Disabled QML element

I have a widget that is widely used in my QML Application, it's enabled property has been used in most of the places to change it's view and disable it's actions. Let's say it is something like this
Item {
MouseArea {
anchors.fill: parent
onClicked: doSomething();
}
}
Now the problem is that we need to show a message when the button is disabled, but MouseArea will also be disabled when the parent is disabled. Is there any workaround for this to force the child to remain enabled?
Actually, the mouse area enabled property works differently from typical Items. It's really not disabled, but seems that way because the parent item is not enabled and thus cannot accept certain mouse events. To solve your problem just make the MouseArea a sibling.

Placeholder text in QML TextEdit

I am looking for a way to show a text hint stating the expected input as advice for the user. Take the Google search bar for example:
Is there a property I am missing, or is this something that has to be achieved through scripting?
The property doesn't exist on the Qt Quick input items. You can vote for the feature here.
In the meantime, you can use TextArea from Qt Quick Controls 2.
If you would rather use pure Qt Quick, you can do something similar to what the Controls do and add a Text item above the field:
import QtQuick 2.5
import QtQuick.Window 2.2
Window {
width: 300
height: 300
visible: true
TextEdit {
id: textEdit
width: 200
height: 50
property string placeholderText: "Enter text here..."
Text {
text: textEdit.placeholderText
color: "#aaa"
visible: !textEdit.text
}
}
}
This is kinda old but I found another necessity for Android builds.
Since Android only send the text editing signal after you press ok in virtual keyboard, the placeholder remains there. So to avoid it, I recommend:
TextEdit {
id: textEdit
width: 200
height: 50
property string placeholderText: "Enter text here..."
Text {
text: textEdit.placeholderText
color: "#aaa"
visible: !textEdit.text && !textEdit.activeFocus // <----------- ;-)
}
}
If you want one line input then why not use TextField ?

LESSCSS: Assign a value to a property taken from another one's

In some cases is common to use same values in different properties, for example (is just an example to show purpose) the following nested rule:
.button-link
{
height:40px;
a
{
line-height:40px;
}
}
The idea is that to vertically center button text line-height and height should be equal.
Is there a way in LESS to "assign a value taken from a diffent property"?
I know that I should use a LESS #variable but in this case is not the same thing and need extra code. Instead should very interesting and useful if I should edit only button's height and then LESS will replaced the same value to line-height
UPDATE:
Another example could be the following:
.button-link
{
color:white;
background:black;
&:hover
{
color:black;
background:white;
}
}
In which "hover" status should invert color and background-color comparing to default state.
This is possible starting with v3 of LESS! Here is the documentation on it.
The example use case they provide ends up with the background-color getting the same value as the color property when compiled:
.widget {
color: #efefef;
background-color: $color;
}
You can´t :(. What i usually do is:
#buttom-height = 100px;
#a-link-height: #buttom-height;
and use that variables in your less declarations. Its a dummy example, i know, but imagine calculated data values from other variables or complex dependencies, proportional paddings/margins... that´s the way i learnt from Bootstrap LESS code.