Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QWidget::setAttribute does not work with attribute Qt::WA_InputMethodEnabled as expected on Windows
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
qinputmethodeveqeventqwidgetqtextedit
4 Posts 2 Posters 5.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AnkurSharmaA Offline
    AnkurSharmaA Offline
    AnkurSharma
    wrote on last edited by
    #1

    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();
    }

    kshegunovK 1 Reply Last reply
    0
    • AnkurSharmaA AnkurSharma

      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();
      }

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

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

      Read and abide by the Qt Code of Conduct

      AnkurSharmaA 1 Reply Last reply
      0
      • kshegunovK kshegunov

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

        AnkurSharmaA Offline
        AnkurSharmaA Offline
        AnkurSharma
        wrote on last edited by
        #3

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

        kshegunovK 1 Reply Last reply
        0
        • AnkurSharmaA AnkurSharma

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

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

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

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved