【问题标题】:QTranslator: why are some parts of app not translated?QTranslator:为什么app的某些部分没有翻译?
【发布时间】:2019-03-06 05:06:40
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

    imagecropwindow_p.h

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-24
      • 2011-12-18
      • 2021-05-19
      • 2011-03-01
      • 1970-01-01
      • 2020-09-12
      相关资源
      最近更新 更多