简体   繁体   English

自定义小部件中的Qt和死键

[英]Qt and dead keys in a custom widget

Issue 问题

I'm unable to make dead keys work in my Qt program, while on the same system Qt applications (konsole or kmail for instance) are correctly processing them. 我无法在我的Qt程序中使用死键,而在同一系统上,Qt应用程序(例如konsole或kmail)正确处理它们。

How to reproduce 如何重现

testcase.pro testcase.pro

TEMPLATE = app
TARGET = testcase
INCLUDEPATH += .
QT += core widgets gui

HEADERS += testcase.hpp
SOURCES += testcase.cpp

testcase.hpp testcase.hpp

#include <QWidget>

class TestWindow: public QWidget
{
    Q_OBJECT
public:
    TestWindow(QWidget* parent=0, Qt::WindowFlags flags=0);
    void keyPressEvent(QKeyEvent* event);
};

testcase.cpp testcase.cpp

#include <QApplication>
#include <QDebug>
#include <QWidget>
#include <QKeyEvent>

#include "testcase.hpp"

TestWindow::TestWindow(QWidget* parent, Qt::WindowFlags flags)
        : QWidget(parent, flags)
{
    setAttribute(Qt::WA_KeyCompression);
}

void TestWindow::keyPressEvent(QKeyEvent* event)
{
    qDebug() << event;
}

int main(int argc, char** argv)
{
    QApplication app(argc, argv);

    TestWindow mainWin;
    mainWin.show();

    return app.exec();
}

Compile the above program (qmake; make), launch it. 编译上面的程序(qmake; make),启动它。 Dead keys give for instance: 死键给出了例如:

QKeyEvent(KeyPress, 1001252, 0, ""^"", false, 1) 
QKeyEvent(KeyPress, 45, 0, ""e"", false, 1) 

I was expecting 我在期待

QKeyEvent(KeyPress, 234, 0, ""ê"", false, 1) 

This would also be acceptable: 这也是可以接受的:

QKeyEvent(KeyPress, 1001252, 0, ""^"", false, 1) 
QKeyEvent(KeyPress, 234, 0, ""ê"", false, 1) 

What I've tried 我试过的

I'm using a Ubuntu 14.10 system with locale fr_FR.UTF-8 我正在使用带有语言环境fr_FR.UTF-8的Ubuntu 14.10系统

I've tried 我试过了

  • with Qt 5.3.0 and Qt 4.8.6 as provided on the system. 系统提供的Qt 5.3.0和Qt 4.8.6。

  • unseting XMODIFIERS (the default value is @im=ibus is being reported an issue by some) unseting XMODIFIERS (默认值为@im=ibus被某些人报告为问题)

  • changing the locale (again, google find reports were the part after the dot is an issue, I've tried the 4 variants UTF-8 , utf-8 , UTF8 and utf8 ) 更改区域设置(再次,谷歌查找报告是点后的部分是一个问题,我尝试了4种变体UTF-8utf-8UTF8utf8

  • with and without the setAttribute(Qt::WA_KeyCompression); 有和没有setAttribute(Qt::WA_KeyCompression); in the constructor. 在构造函数中。

None changed my observable behavior. 没有改变我的可观察行为。

Searching the web show mainly (only?) system related issues. 搜索网络主要(仅?)系统相关问题。 As written above, I've tried the proposed solution, that doesn't solve my problem and the fact that other Qt applications I've tried are able to process the dead key makes me think I miss something in my code, especially that with a slightly more complex example, I'm able to use dead keys with Qt provided widgets (for instance a QLineEdit). 如上所述,我尝试了所提出的解决方案,但这并没有解决我的问题以及我尝试过的其他Qt应用程序能够处理死键的事实让我觉得我错过了我的代码中的某些内容,尤其是一个稍微复杂的例子,我可以使用Qt提供的小部件的死键(例如QLineEdit)。

This is a partial answer, but as nobody answered that could be useful for someone later. 这是一个部分答案,但是没有人回答这对以后的人有用。

Dead keys are handled by input methods which are also handling the compose key and the way Chinese characters and others may be entered. 死键由输入方法处理,输入方法也处理组合键以及输入汉字和其他字符的方式。

The widget must signify that it handles input method: 小部件必须表示它处理输入方法:

setAttribute(Qt::WA_InputMethodEnabled, true);

Then it has to overwrite two virtual members: 然后它必须覆盖两个虚拟成员:

void inputMethodEvent(QInputMethodEvent*);
QVariant inputMethodQuery(Qt::InputMethodQuery) const;

To handle the dead keys and compose, it seems to be enough to have 为了处理死键和组合,似乎已经足够了

void TestWindow::inputMethodEvent(QInputMethodEvent* event)
{
    if (!event->commitString().isEmpty()) {
        QKeyEvent keyEvent(QEvent::KeyPress, 0, Qt::NoModifier,
                           event->commitString());
        keyPressEvent(&keyEvent);
    }
    event->accept();
}

QVariant TestWindow::inputMethodQuery(Qt::InputMethodQuery) const
{
    return QVariant();
}

but that's were my answer is incomplete: 但那是我的答案是不完整的:

  1. I'm not sure it is really enough even for that 即便如此,我也不确定它是否足够

  2. I'm sure it is not enough for more complex writing system and I lack the prerequisite to understand the documentation I've found. 我确信这对于更复杂的书写系统是不够的,我缺乏理解我发现的文档的先决条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM