QKeyEvent::text() doesn't return accent letter on Linux

243 Views Asked by At

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.

1

There are 1 best solutions below

4
eyllanesc On BEST ANSWER

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