1

我在 PyQt5 中使用site-packages/PyQt5/translations文件夹中的 qt_xx.qm 的 QTextEdit、QPlainTextEdit 和 QTextBrowser 的翻译默认上下文菜单有问题。例如,它适用于 QLineEdit(这是相同的上下文菜单)。如何处理?

重现问题的代码:

# coding: utf-8

import sys

from PyQt5.QtCore import QLocale
from PyQt5.QtCore import QTranslator
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QTextEdit
from PyQt5.QtWidgets import QVBoxLayout


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        widget = QWidget()

        lineEdit = QLineEdit()
        textEdit = QTextEdit()

        vbox = QVBoxLayout(widget)
        vbox.addWidget(lineEdit)
        vbox.addWidget(textEdit)

        self.setCentralWidget(widget)


if __name__ == '__main__':

    app = QApplication(sys.argv)
    locale = QLocale.system().name()

    qtTranslator = QTranslator()
    if qtTranslator.load('C:/Python33/Lib/site-packages/PyQt5/translations/qt_' + locale):
        app.installTranslator(qtTranslator)

    mainWindow = MainWindow()
    mainWindow.show()
    app.exec_()
4

2 回答 2

0

我遇到了同样的问题,我尝试通过修改调用方法创建的标准上下文菜单来解决问题QTextEdit::createStandardContext,它确实有效。
这是代码:

void CWQTextEdit::contextMenuEvent(QContextMenuEvent *e)
{
    QMenu *menu = QTextEdit::createStandardContextMenu();
    if (menu == NULL)
        return QTextEdit::contextMenuEvent(e);

    QList<QAction*> actions = menu->actions();

    foreach (QAction *action, actions)
    {
       QString actionText = action->text();
       if (actionText.contains("Undo"))
       {
           actionText.replace("Undo", tr("Undo"));
           action->setText(actionText);
       }
       else if (actionText.contains("Redo"))
       {
           actionText.replace("Redo", tr("Redo"));
           action->setText(actionText);
       }
       else if (actionText.contains("Cu&t"))
       {
           actionText.replace("Cu&t", tr("Cut"));
           action->setText(actionText);
       }
       else if (actionText.contains("Copy"))
       {
           actionText.replace("Copy", tr("Copy"));
           action->setText(actionText);
       }
       else if (actionText.contains("Paste"))
       {
           actionText.replace("Paste", tr("Paste"));
           action->setText(actionText);
       }
       else if (actionText.contains("Delete"))
       {
           actionText.replace("Delete", tr("Delete"));
           action->setText(actionText);
       }
       else if (actionText.contains("Select All"))
       {
           actionText.replace("Select All", tr("Select All"));
           action->setText(actionText);
       }
    }

    menu->exec(e->globalPos());
}
于 2014-08-12T04:29:25.100 回答
0
if __name__ == "__main__":
    app = QApplication(argv)
    translator = QtCore.QTranslator()
    translator.load("translations/qt_zh_CN.qm")
    app.installTranslator(translator)
于 2018-05-28T07:22:50.080 回答