Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

(Difference between revisions)

Workaround to hide VKB in QML apps (Known Issue)

From Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Text replace - "Category:MeeGo 1.2 Harmattan" to "")
hamishwillee (Talk | contribs)
m (Text replace - "Category:MeeGo" to "Category:MeeGo Harmattan")
Line 1: Line 1:
[[Category:Qt Mobility]][[Category:UI]][[Category:Qt]][[Category:MeeGo]][[Category:Code Snippet]][[Category:Known Issue]]
+
[[Category:Qt Mobility]][[Category:UI]][[Category:Qt]][[Category:MeeGo Harmattan]][[Category:Code Snippet]][[Category:Known Issue]]
 
{{Abstract|This article explains how to close the virtual keyboard in pure QML apps.}}  
 
{{Abstract|This article explains how to close the virtual keyboard in pure QML apps.}}  
 
{{Archived|timestamp=20120220225828|user=[[User:Hamishwillee|<br />----]]|This bug has been fixed and is released in Harmattan > PR 1.1}}
 
{{Archived|timestamp=20120220225828|user=[[User:Hamishwillee|<br />----]]|This bug has been fixed and is released in Harmattan > PR 1.1}}

Revision as of 10:58, 13 June 2012

This article explains how to close the virtual keyboard in pure QML apps.

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

This bug has been fixed and is released in Harmattan > PR 1.1

Article Metadata
Article
Created: gnuton (28 Oct 2011)
Last edited: hamishwillee (13 Jun 2012)

Contents

Introduction

Pure QML applications on MeeGo Harmattan have an annoying bug in which the Virtual Keyboard (VKB) is not closed when you tap outside a selected text editing element.

What happens is that when a QML element that is able to listen for key events receives the focus, a virtual keyboard is displayed that the user can use for entering text. When the user taps outside the element to indicate they have finished entering text the element doesn't actually lose the focus, so the keyboard is not closed. This goes against the Harmattan style guidelines (and common sense).

This article describes a number of workarounds to the problem.

Pure QML Solution

One workaround is to use closeSoftwareInputPanel QML method. The example below is the implementation of this fix inside a simple QML app made up of a root Rectangle element and a TextEdit. The virtual keyboard is shown when the TextEdit is clicked, but the virtual keyboard is not hidden when the user taps outside.

The main problem here is related to the focus. Tapping outside the TextEdit element is not enough to change the focus and close the VKB, so we have to add some additional code. The fix is implemented basically in two parts:

  • MouseArea - a big and transparent to events MousArea overlays the entire application. This allows us to give the focus to the clicked item
  • onActiveFocusChanged slot inside the any element able to receive text input. This slot actually closes the VKB

The rest of example is pretty simple and the complete code can be seen below:

import QtQuick 1.0
Rectangle {
id: root
width: 800
height: 600
focus: true
Rectangle {
width: 360
height: 360
color: "cyan"
TextEdit {
id: mText
anchors.fill: parent
onActiveFocusChanged: {
if (!mText.activeFocus) mText.closeSoftwareInputPanel();
}
}
}
MouseArea {
anchors.fill: parent
onPressed: {
// Let the mousePress events go to below items
mouse.accepted = false;
// Clicked item below the MouseArea gets the focus
var selectedItem = root.childAt(mouse.x, mouse.y);
if (!selectedItem) selectedItem = root;
selectedItem.focus = true;
}
}
}

C++ workaround [BETTER SOLUTION]

A pure C++ workaround consists of adding an event filter to the affected view instance:

class EventFilter : public QObject 
{
protected:
bool eventFilter(QObject *obj, QEvent *event) {
QInputContext *ic = qApp->inputContext();
if (ic) {
if (ic->focusWidget() == 0 && prevFocusWidget) {
QEvent closeSIPEvent(QEvent::CloseSoftwareInputPanel);
ic->filterEvent(&closeSIPEvent);
} else if (prevFocusWidget == 0 && ic->focusWidget()) {
QEvent openSIPEvent(QEvent::RequestSoftwareInputPanel);
ic->filterEvent(&openSIPEvent);
}
prevFocusWidget = ic->focusWidget();
}
return QObject::eventFilter(obj,event);
}
 
private:
QWidget *prevFocusWidget;
};

This filter can be installed on a QGraphicsView or (in QML) on a QDeclarativeView instance:

EventFilter ef; 
view.installEventFilter(&ef);

MeeGo Components are not affected!

While this bug affects plain QML apps, MeeGo component based apps are not affected! So if possible you should use the Meego elements set instead of the standard QML ones. The only one drawback here is the compatibility with Symbian set. In fact, if your target is to run the app both on Symbian and MeeGo polatforms then the second solution shown above is the right way to go.

106 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×