Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QWidget::setAttribute does not work with attribute Qt::WA_InputMethodEnabled as expected on Windows
AnkurSharma last edited by
I am working on a text editor application. Where I have a QWidget on which I render the text input by user. On some scenarios I want to disable the capability of this widget to accept input methods. So when I want to disable the input methods, I invoke:
setAttribute(Qt::WA_InputMethodEnabled attribute, false);
for this widget. But what I noticed that, even after invoking this function I still keep on getting the input method events which I should have not based on the documentation which says following about this attribute: "Qt::WA_InputMethodEnabled : Enables input methods for Asian languages. Must be set when creating custom text editing widgets."
The same code works as expected on OSX. I created a sample project to emulate the behavior and I realized that on Windows all the QWidgets keep on getting the inputmethod events even when user explicitly disables it.
So question is am I missing something or is it like a Qt bug and if so is there a workaround for this.?
Following is the code for the sample application:
class MyWdiget :
bool eventFilter( QObject * /inObject/, QEvent * inEvent )
switch ( inEvent->type() )
QVariant inputMethodQuery ( Qt::InputMethodQuery ) const
if( Qt::InputMethodQuery == )
int main(int argc, char *argv)
QApplication a(argc, argv);
QHBoxLayout *layout = new QHBoxLayout;
MyWdiget *pNew = new MyWdiget;
pNew->installEventFilter( pNew );
First of all, if I recall correctly, some attributes can be set and/or cleared before the widget is created; and second of all, why are you handling the events with a event filter? You should rather override QWidget::inputMethodEvent.
As you're doing it now, the event filter is executed before the widget has even touched the event (event filters are on the level of
QObject) so you don't allow for the widget to actually handle the
QInputMethodEventand to respect the attribute.
AnkurSharma last edited by
@kshegunov Thanks for the response.
I agree to what you say about overriding the inputMethodEvent rather than using the event filter.
But, I also expect that when I am resetting the attribute of WA_InputMethodEvent my widget should no more get the inputmethod events anymore, and thats how it works on Qt on OSX.
The same program runs as expected on Qt 5.3 on OSX.
Even if I create a bare window the way I have done and reset that attribute explicitly, and Don't override the inputMthodEvent or won't use event filter then again, I can see my widget getting the input method events, which seems incorrect to me. How do I avoid that.?
But, I also expect that when I am resetting the attribute of WA_InputMethodEvent my widget should no more get the input method events anymore, and thats how it works on Qt on OSX.
I don't know how it works on OSX, I can say for sure though that
QObject::eventFilteris executed before the
QObject::eventmethod (this is documented) and since you're not giving Qt leeway to process the event as it ordinarily would in the overriden
QWidget::eventmethod (you stop the event propagation when you return
truefrom the event filter) you shouldn't expect it to respect the widget attribute. That's what I'm claiming. You should move your code from the
eventFiltermethod to a