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