QTranslator:为什么应用程序的某些部分未翻译?

时间:2018-10-01 05:43:59

标签: c++ qt qt5 qt-linguist qtranslator

有一个正在运行的应用程序,我正在为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*** 

1 个答案:

答案 0 :(得分:3)

QTranslator使用MOC进行翻译,因此如果您希望翻译小部件,则应使用宏Q_OBJECT,在您的情况下为{{1} }没有它,因此解决方案是添加它:

imagecropwindow_p.h

CropWindowComponents

enter image description here

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