1

Does anyone know why QKeyEvent::text() for typing ` + a returns one empty string and one letter a instead of one empty string and à on Linux? Under Windows this seems to be working fine (same application running under Windows and Linux).

I'm handling the pressed keys via this class.

Damir Porobic
  • 681
  • 1
  • 8
  • 21

1 Answers1

1

You have to enable the Qt::WA_InputMethodEnabled attribute in addition to override the inputMethodEvent method:

#include <QtWidgets>

class Widget: public QWidget{
public:
    Widget(QWidget *parent=nullptr): QWidget(parent){
        setAttribute(Qt::WA_InputMethodEnabled, true);
    }
protected:
    void keyPressEvent(QKeyEvent *event){
        qDebug() << "keyPressEvent" << event->text();
        QWidget::keyPressEvent(event);
    }
    void inputMethodEvent(QInputMethodEvent *event){
        qDebug() << "inputMethodEvent" << event->commitString();
        QWidget::inputMethodEvent(event);
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thanks for the feedback. Unfortunately I'm not using a Widget for text input but a Class that Inherits from QGraphicsItem. Setting Qt::WA_InputMethodEnabled gives a console entry saying Attribute not supported. I have overwritten the method that you mentioned but it}s not getting called. – Damir Porobic Feb 02 '20 at 12:30
  • I think I got it, it should be `setFlag(QGraphicsItem::ItemAcceptsInputMethod, true);` It looks like the method gives me the correct text now. Need to double check. – Damir Porobic Feb 02 '20 at 12:36
  • Yes, working now. The keyPressEvent is triggered for the usual Latin letters, when an composite letter occurs, the InputMethod is triggered. Thanks for your help. – Damir Porobic Feb 02 '20 at 13:17
  • Oh, this also Fixes Chinese Character Input, which was not working before in my case and seems to be working now. – Damir Porobic Feb 04 '20 at 11:45