[Interest] TextField QML auto keyboard hiding/lose focus on touch events

mark diener rpzrpzrpz at gmail.com
Wed Jan 27 02:06:00 CET 2016


Gian:

Ok, I wanted to share my solution since you kindly offered a
suggestion a day or two ago.

1) Create an C++ function: Q_INVOKABLE void gregister(QObject* goedit) ;

2) Store the QObject away some where for access ONLY on the GUI thread.

3) For controls that I want to LOSE focus when click anywhere OUTSIDE
of that control, I call the register with the ID.

For example) QML: onFocusChanged: if (gtextfield.focus == true)
Mysingleton.gregister(gtextfield) ;

4) Install an event filter:

 QGuiApplication->installEventFilter(&gfilter);

Inside the event filter, I do the following:

A) Ignore messages from Qobject* obj->isWindowType() -> return false;
B) Only look for  QEvent::MouseButtonPress and QEvent::TouchBegin events
C) Use Qmetaobject*->classname() to check if the name of the obj
pointer is a pointer to ANOTHER text input field using
QString.contains() == true
D) If find your classname in the parent tree, then just return false,
do nothing, since another input control is going to appear that needs
the virtual keyboard, no sense in hiding it.
E) Make sure you check the parent tree since your class name may appear higher
F) gtextfield->setProperty("focus",QVariant(false)) -> Removes focus
on your control and hides the Virtual keyboard.

This works beautifully and is a code pattern that scales without
regard to how big your QML object->parent tree is.

And the message filter is very efficient, does not add any real
overhead to the message stack tree.

Thanks,

Md





On Mon, Jan 25, 2016 at 12:48 PM, md at rpzdesign.com <md at rpzdesign.com> wrote:
> Gian:
>
> Thank you for the input.
>
> I tried it a little bit and found that mousearea onPressed would not trigger
> up past Loader, so as a universal pattern it will not work.
>
> In fact, many controls (Listview,etc) suck up the mouse events when those
> mouse/touch events SHOULD cause the keyboard to retract.
>
> I am going to put a registration into my custom controls that application
> wide will register themselves -> onFocusFocusChanged:
> C++Invokable(thisqmlobject,thisqmlobject.activefocus)
> and every time a specific control gains focus, it will register itself and
> then I will filter on events to compare the QMouseEvent/QTouchEvent with the
> registered QObject* (Qml Item) to see whether the keyboard should retract.
>
> This will separate my code from all the event snarfing components in the QML
> item stack.
>
> I think this is a PC legacy behavior not suited to touch.
>
> Thanks,
>
> md
>
> On 1/25/2016 11:20 AM, Gian Maxera wrote:
>>
>> Hello Mark,
>> I achieve that on mobile using a simple MouseArea that cover all the
>> screen and it’s under all other active elements.
>> When that MouseArea is touched, I call Qt.inputMethod.hide() … and that’s
>> it.
>> So, if the user touch outside any button, textfield, etc… then that touch
>> will reach the underlying MouseArea that will call the hide on keyboard.
>>
>> Ciao,
>> Gianluca.
>>
>>
>>> On 25 Jan 2016, at 17:16, mark diener <rpzrpzrpz at gmail.com> wrote:
>>>
>>> Hello List:
>>>
>>> Anybody have a tip or suggestion for IOS/Android Qt 5.5.1 -> Qt 5.6.0
>>> Beta
>>>
>>> I am using a TextField and the keyboard pops up whenever the input
>>> control
>>> receives focus.
>>>
>>> But I want the TextField to lose focus whenever you click/touch
>>> outside of the control.
>>>
>>> Not Just Tab & Enter that are VERY PC centric, not tablet/smartphone
>>> concepts.
>>>
>>> Right now, the blinking cursor remains on the field and the
>>> Android/IOS keyboard remains visible
>>> when you use your finger and touch some where outside of the TextField.
>>>
>>> Obviously, I want the keyboard to REMAIN if the user clicks on ANOTHER
>>> TextField.
>>>
>>> Is there a QML way to do this?
>>>
>>> I have some C++ and event filter ideas to make this happen, but those
>>> are brute force ideas and there might be something that I don't see in
>>> the QML TextField documentation.
>>>
>>> Thanks,
>>>
>>> md
>>> _______________________________________________
>>> Interest mailing list
>>> Interest at qt-project.org
>>> http://lists.qt-project.org/mailman/listinfo/interest
>>
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/interest
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest



More information about the Interest mailing list