简体   繁体   English

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

[英]QTranslator: why are some parts of app not translated?

There is a working app and I am adding a new language for GUI. 有一个工作的应用程序,我正在为GUI添加一种新的语言。 Everything works fine but some part of the app are simply not translated. 一切正常,但应用程序的某些部分根本没有翻译。 QLinguist detects all of them, and I have added a new translations, but still no result. QLinguist检测到所有这些,我添加了新的翻译,但仍然没有结果。

Here is the fragment of the code that is not getting translation: 以下是未获得翻译的代码片段:

"imagecropwindow_p.h" : “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" : “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" : “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" : “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());
}

And "main.cpp" : “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();
}

You can find the whole project here . 你可以在这里找到整个项目。

PS: For an example case, I added stars (*) as a new translation, such as PS:对于一个示例案例,我添加了星号(*)作为新的翻译,例如

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

QTranslator uses the MOC to do the translations so if you want that if you want your widget to be translated you should use the macro Q_OBJECT , in your case CropWindowComponents does not have it, so the solution is to add it: QTranslator使用MOC进行翻译,所以如果你想要翻译你的小部件,你应该使用宏Q_OBJECT ,在你的情况下CropWindowComponents没有它,所以解决方案是添加它:

imagecropwindow_p.h imagecropwindow_p.h

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

在此输入图像描述

On the other hand do not add the .ts to the .qrc since the .ts only serves to convert it to the .qm binary. 另一方面,不要将.ts添加到.qrc,因为.ts仅用于将其转换为.qm二进制文件。 When you add a file to the .qrc it is compiled and added to the executable, increasing the size of the last one. 将文件添加到.qrc时,会将其编译并添加到可执行文件中,从而增加最后一个文件的大小。 Therefore adding the .ts increases the size of the executable unnecessarily. 因此,添加.ts会不必要地增加可执行文件的大小。

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

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