The Input Panel example shows how to create an input panel that can be used to input text into widgets using only the pointer and no keyboard.
The input fields in the main window have no function other than to accept input. The main focus is on how the extra input panel can be used to input text without the need for a real keyboard or keypad.
Main Form Class Definition
Because the main window has no other function than to accept input, it has no class definition. Instead, its whole layout is made in Qt Designer. This emphasizes the point that no widget specific code is needed to use input panels with Qt.
The MyInputPanelContext class inherits QInputContext, which is Qt's base class for handling input methods. MyInputPanelContext is responsible for managing the state of the input panel and sending input method events to the receiving widgets.
The inputPanel member is a pointer to the input panel widget itself; in other words, the window that will display the buttons used for input.
The identifierName(), language(), isComposing() and reset() functions are there mainly to fill in the pure virtual functions in the base class, QInputContext, but they can be useful in other scenarios. The important functions and slots are the following:
filterEvent() is where we receive events telling us to open or close the input panel.
sendCharacter() is a slot which is called when we want to send a character to the focused widget.
updatePosition() is used to position the input panel relative to the focused widget, and will be used when opening the input panel.
MyInputPanelContext Class Implementation
In the constructor we connect to the characterGenerated() signal of the input panel, in order to receive key presses. We'll see how it works in detail later on.
MyInputPanelContext::MyInputPanelContext()
{
inputPanel = new MyInputPanel;
connect(inputPanel, SIGNAL(characterGenerated(QChar)), SLOT(sendCharacter(QChar)));
}
In the filterEvent() function, we must look for the two event types: RequestSoftwareInputPanel and CloseSoftwareInputPanel.
The first type will be sent whenever an input capable widget wants to ask for an input panel. Qt's input widgets do this automatically. If we receive that type of event, we call updatePosition() — we'll see later on what it does — then show the actual input panel widget. If we receive the CloseSoftwareInputPanel event, we do the opposite, and hide the input panel.
void MyInputPanelContext::sendCharacter(QChar character)
{
QPointer<QWidget> w = focusWidget();
if (!w)
return;
QKeyEvent keyPress(QEvent::KeyPress, character.unicode(), Qt::NoModifier, QString(character));
QApplication::sendEvent(w, &keyPress);
if (!w)
return;
QKeyEvent keyRelease(QEvent::KeyPress, character.unicode(), Qt::NoModifier, QString());
QApplication::sendEvent(w, &keyRelease);
}
We implement the sendCharacter() function so that it sends the supplied character to the focused widget. All QInputContext based classes are always supposed to send events to the widget returned by QInputContext::focusWidget(). Note the QPointer guards to make sure that the widget does not get destroyed in between events.
Also note that we chose to use key press events in this example. For more complex use cases with composed text it might be more appropriate to send QInputMethodEvent events.
The updatePosition() function is implemented to position the actual input panel window directly below the focused widget.
If we look at the member variables first, we see that there is form, which is made with Qt Designer, that contains the layout of buttons to click. Note that all the buttons in the layout have been declared with the NoFocus focus policy so that we can maintain focus on the window receiving input instead of the window containing buttons.
The lastFocusedWidget is a helper variable, which also aids in maintaining focus.
signalMapper is an instance of the QSignalMapper class and is there to help us tell which button was clicked. Since they are all very similar this is a better solution than creating a separate slot for each one.
The functions that we implement in MyInputPanel are the following:
event() is used to intercept and manipulate focus events, so we can maintain focus in the main window.
saveFocusWidget() is a slot which will be called whenever focus changes, and allows us to store the newly focused widget in lastFocusedWidget, so that its focus can be restored if it loses it to the input panel.
buttonClicked() is a slot which will be called by the signalMapper whenever it receives a clicked() signal from any of the buttons.
MyInputPanel Class Implementation
If we look at the constructor first, we have a lot of signals to connect to!
We connect the QApplication::focusChanged() signal to the saveFocusWidget() signal in order to get focus updates. Then comes the interesting part with the signal mapper: the series of setMapping() calls sets the mapper up so that each signal from one of the buttons will result in a QSignalMapper::mapped() signal, with the given widget as a parameter. This allows us to do general processing of clicks.
The next series of connections then connect each button's clicked() signal to the signal mapper. Finally, we create a connection from the mapped() signal to the buttonClicked() slot, where we will handle it.
In the buttonClicked() slot, we extract the value of the "buttonValue" property. This is a custom property which was created in Qt Designer and set to the character that we wish the button to produce. Then we emit the characterGenerated() signal, which MyInputPanelContext is connected to. This will in turn cause it to send the input to the focused widget.
In the saveFocusWidget() slot, we test whether the newly focused widget is a child of the input panel or not, using the QWidget::isAncestorOf() call.
If it isn't, it means that the widget is outside the input panel, and we store a pointer to that widget for later.
In the event() function we handle QEvent::WindowActivate event, which occurs if the focus switches to the input panel.
case QEvent::WindowActivate:
if (lastFocusedWidget)
lastFocusedWidget->activateWindow();
break;
Since we want avoid focus on the input panel, we immediately call QWidget::activateWindow() on the widget that last had focus, so that input into that widget can continue. We ignore any other events that we receive.
Setting the Input Context
The main function for the example is very similar to those for other examples. The only real difference is that it creates a MyInputPanelContext and sets it as the application-wide input context.
With the input context in place, we set up and show the user interface made in Qt Designer before running the event loop.
Further Reading
This example shows a specific kind of input context that uses interaction with a widget to provide input for another. Qt's input context system can also be used to create other kinds of input methods. We recommend starting with the QInputContext documentation if you want to explore further.
Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).
Qt dans le magazine
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter
ou par MP !