QWidget::setAttribute does not work with attribute Qt::WA_InputMethodEnabled as expected on Windows



  • 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:

    #include <QtGui/QApplication>
    #include <QWidget>
    #include <QHBoxLayout>
    #include <QEvent>
    #include <QInputMethodEvent>
    #include <QDebug>
    class MyWdiget :
    public QWidget
    {
    public:
    MyWdiget()
    {
    }
    protected:
    bool eventFilter( QObject * /inObject/, QEvent * inEvent )
    {
    switch ( inEvent->type() )
    {
    case QEvent::InputMethod:
    qDebug()<<"InputMethod";
    return true;
    default:
    return false;
    }
    }
    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 );
    pNew->setAttribute(Qt::WA_InputMethodEnabled,true);
    pNew->setLayout(layout);
    pNew->show();
    return a.exec();
    }


  • Qt Champions 2016

    @AnkurSharma
    Hello,
    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 QInputMethodEvent and to respect the attribute.

    Kind regards.



  • @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.?


  • Qt Champions 2016

    @AnkurSharma

    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::eventFilter is executed before the QObject::event method (this is documented) and since you're not giving Qt leeway to process the event as it ordinarily would in the overriden QWidget::event method (you stop the event propagation when you return true from 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 eventFilter method to a QWidget::inputMethodEvent override.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.