【问题标题】:how to translate key shortcut如何翻译快捷键
【发布时间】:2021-10-06 16:35:09
【问题描述】:

我不能强制 QKeySequence::toString() 返回翻译后的快捷方式表示,尽管它的文档表明它应该可以工作。文档说:“字符串、“Ctrl”、“Shift”等是在“QShortcut”上下文中使用 QObject::tr() 翻译的。”但我不完全确定快捷方式上下文是什么意思。我可能做错了什么......

这是我的例子。为了使它工作,我需要将qtbase_es.qm 从 Qt 安装目录复制到我的项目构建目录。正确加载翻译后,菜单中的操作会正确显示“Action Control+Intro”,这是“Action Ctrl+Enter”快捷键的西班牙语翻译。但是主窗口上的工具提示仍然是“Action (Ctrl+Enter)”。我希望它是“Action (Control+Intro)”,就像在菜单中一样。我做错了什么?

#include <QAction>
#include <QApplication>
#include <QDebug>
#include <QMainWindow>
#include <QMenuBar>
#include <QTranslator>

int main(int argc, char *argv[])
{
    QTranslator spanish;
    qDebug() << spanish.load("qtbase_es.qm"); // should return true if correctly loaded
    QApplication a(argc, argv);
    QApplication::installTranslator(&spanish);
    QMainWindow w;
    auto menu = new QMenu("Menu");
    auto action = menu->addAction("Action");
    action->setShortcutContext(Qt::ApplicationShortcut);
    action->setShortcut(Qt::CTRL | Qt::Key_Enter);
    w.menuBar()->addMenu(menu);
    w.show();
    QApplication::processEvents(); // I also tried this line but it is useless...
    w.setToolTip(QString("%1 (%2)").arg(action->text(), action->shortcut().toString()));
    qDebug() << action->shortcut().toString(); // WRONG: returns Ctrl+Enter but I expect Control+Intro
    return a.exec();
}

【问题讨论】:

    标签: c++ qt translation


    【解决方案1】:

    QShortcut::toString 有一个SequenceFormat 参数,默认为ProtableText。格式的文档指出,该可移植格式适用于例如写入文件。

    本机格式旨在向用户显示,只有这种格式才能执行翻译。

    试试:

    qDebug() << action->shortcut().toString(QKeySequence::NativeText);
    

    【讨论】:

    • 完美,有效。可惜我没有尝试这个参数。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 2011-06-07
    相关资源
    最近更新 更多