QTranslator:为什么app有些地方没有翻译?

QTranslator: why are some parts of app not translated?

有一个可用的应用程序,我正在为 GUI 添加一种新语言。一切正常,但应用程序的某些部分根本没有翻译。 QLinguist 都检测出来了,我又添加了新的翻译,还是没有结果。

以下是未翻译的代码片段:

"imagecropwindow_p.h":

#include <QWidget>

class QLabel;
class QPushButton;
class QHBoxLayout;
class QVBoxLayout;
class QFrame;

class CropWindowComponents: public QWidget
{
public:
    CropWindowComponents(QWidget *parent = nullptr);

    QPushButton *changeBtn;
    QPushButton *cropBtn;
    QPushButton *continueBtn;
    QPushButton *cancelBtn;
};

class HorizontalWindow : public CropWindowComponents
{
    Q_OBJECT
public:
    HorizontalWindow(QWidget *parent = nullptr);
};

class VerticalWindow : public CropWindowComponents
{
    Q_OBJECT
public:
    VerticalWindow(QWidget *parent = nullptr);
};

"imagecropwindow_p.cpp":

#include "imagecropwindow_p.h"

#include <QLabel>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>

CropWindowComponents::CropWindowComponents(QWidget *parent) :
    QWidget(parent)
{
    changeBtn = new QPushButton(tr("Change"), this);
    cropBtn = new QPushButton(tr("Crop"), this);
    continueBtn = new QPushButton(tr("Continue"), this);
    cancelBtn = new QPushButton(tr("Cancel"), this);
}

HorizontalWindow::HorizontalWindow(QWidget *parent) :
    CropWindowComponents(parent)
{
    QHBoxLayout *btnsLyt = new QHBoxLayout;
    btnsLyt->setMargin(0);
    btnsLyt->addWidget(changeBtn);
    btnsLyt->addWidget(cropBtn);
    btnsLyt->addWidget(continueBtn);
    btnsLyt->addWidget(cancelBtn);
    btnsLyt->addStretch();

    setLayout(btnsLyt);
}

VerticalWindow::VerticalWindow(QWidget *parent) :
    CropWindowComponents(parent)
{
    QVBoxLayout *btnsLyt = new QVBoxLayout;
    btnsLyt->setMargin(0);
    btnsLyt->addWidget(changeBtn);
    btnsLyt->addWidget(cropBtn);
    btnsLyt->addWidget(continueBtn);
    btnsLyt->addWidget(cancelBtn);
    btnsLyt->addStretch();

    setLayout(btnsLyt);
}

"imagecropperwindow.h":

#include "imagecropwindow_p.h"

class ImageCropperWindow : public QWidget
{
    Q_OBJECT
public:
    explicit ImageCropperWindow(QWidget *parent = nullptr);

private slots:
    void changeWindowOrientation();

private:
    HorizontalWindow *horizWindow;
    VerticalWindow *verticalWindow;
};

"imagecropperwindow.cpp":

#include "imagecropperwindow.h"

#include <QDebug>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>

ImageCropperWindow::ImageCropperWindow(QWidget *parent) : QWidget(parent)
{
    horizWindow = new HorizontalWindow(this);
    verticalWindow = new VerticalWindow(this);

    connect(horizWindow->changeBtn, &QPushButton::clicked,
            this, &ImageCropperWindow::changeWindowOrientation);
    connect(verticalWindow->changeBtn, &QPushButton::clicked,
            this, &ImageCropperWindow::changeWindowOrientation);

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->setMargin(3);
    layout->addWidget(horizWindow);
    layout->addWidget(verticalWindow);
    verticalWindow->setVisible(false);

    setLayout(layout);
}

void ImageCropperWindow::changeWindowOrientation()
{
    if (horizWindow->isVisible()) {
        horizWindow->setVisible(false);
        verticalWindow->setVisible(true);
    }
    else {
        verticalWindow->setVisible(false);
        horizWindow->setVisible(true);
    }

    this->resize(this->minimumSizeHint());
}

"main.cpp":

#include <QApplication>
#include <QDebug>
#include <QTranslator>

#include "imagecropperwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QTranslator newLang;
    newLang.load(":/translations/newLanguage.qm");

    a.installTranslator(&newLang);

    ImageCropperWindow w;
    w.show();

    return a.exec();
}

您可以找到整个项目 here

PS:举个例子,我加了星号(*)作为新的翻译,比如

nativeLanuage  - ***newLanguage***  
Change          - ***Change***  
Crop            - ***Crop***  
Continue        - ***Continue***  
Cancel          - ***Cancel*** 

QTranslator 使用 MOC 进行翻译,所以如果你想要翻译你的小部件,你应该使用宏 Q_OBJECT,在你的情况下 CropWindowComponents没有,解决方法是添加:

imagecropwindow_p.h

class CropWindowComponents: public QWidget
{
    Q_OBJECT # <--- add this
public:
...

另一方面,不要将 .ts 添加到 .qrc,因为 .ts 仅用于将其转换为 .qm 二进制文件。当您将文件添加到 .qrc 时,它会被编译并添加到可执行文件中,从而增加最后一个文件的大小。因此,添加 .ts 会不必要地增加可执行文件的大小。