Qt语言学家-设置应用程序Qt * .ui文件的转换器 [英] Qt Linguist - set translator for application Qt *.ui files

查看:135
本文介绍了Qt语言学家-设置应用程序Qt * .ui文件的转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在菜单中选择一种语言后,我写了一个小巧的示例来更改应用程序的语言.尽管connect DOES可以正常工作(qDebug()打印出良好的消息),但它不会更改QLabel上的文本.我使用QtDesigner创建了GUI. 注意:所有这些文件都在同一目录中.我正在使用Qt5.这是我的代码:

I wrote a tiny, simple example to change applications language after choosing a language in menu. Although connect DOES work (qDebug() prints good messages) it doesnt change a text on my QLabel. I created GUI using QtDesigner. NOTE: All of those files are in the same directory. Im using Qt5. Heres my code:

* .pro:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = qt_pl_en
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

TRANSLATIONS += ic.ts
TRANSLATIONS += se.ts

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QTranslator>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    retranslate();

    QObject::connect(ui->action_Icelandic, SIGNAL(triggered()), this, SLOT(speakIcelandic()));
    QObject::connect(ui->action_Swedish, SIGNAL(triggered()), this, SLOT(speakSwedish()));
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::changeEvent(QEvent* event)
{
    if (event->type() == QEvent::LanguageChange)
    {
        // retranslate designer form
        ui->retranslateUi(this);

        // retranslate other widgets which weren't added in designer
        retranslate();
    }

    // remember to call base class implementation
    QWidget::changeEvent(event);
}

void MainWindow::retranslate()
{
    ui->label->setText(QObject::tr("Hello, world! :-)"));
}

void MainWindow::speakSwedish()
{
    QTranslator translator;
    translator.load("se.qm");
    qApp->installTranslator(&translator);

    ui->retranslateUi(this);

    qDebug() << "Swedish";
}

void MainWindow::speakIcelandic()
{
    QTranslator translator;
    translator.load("ic.qm");
    qApp->installTranslator(&translator);

    ui->retranslateUi(this);

    qDebug() << "Icelandic";
}

mainwindow.h

    #ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

    void retranslate();
    void changeEvent(QEvent* event);


private slots:

    void speakSwedish();
    void speakIcelandic();
};

#endif // MAINWINDOW_H

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

ic.ts

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.0" language="en_EN">
<context>
    <name>MainWindow</name>
    <message>
        <location filename="mainwindow.ui" line="14"/>
        <source>MainWindow</source>
        <translation type="unfinished"></translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="27"/>
        <location filename="mainwindow.cpp" line="13"/>
        <source>Hello, world! :-)</source>
        <translation type="unfinished">Halló, heimur! :-)</translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="42"/>
        <source>File</source>
        <translation type="unfinished"></translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="60"/>
        <source>&amp;Swedish</source>
        <translation type="unfinished"></translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="65"/>
        <source>&amp;Icelandic</source>
        <translation type="unfinished"></translation>
    </message>
</context>
</TS>

se.ts

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.0" language="en_EN">
<context>
    <name>MainWindow</name>
    <message>
        <location filename="mainwindow.ui" line="14"/>
        <source>MainWindow</source>
        <translation type="unfinished"></translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="27"/>
        <location filename="mainwindow.cpp" line="13"/>
        <source>Hello, world! :-)</source>
        <translation type="unfinished">Hallå, världen! :-)</translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="42"/>
        <source>File</source>
        <translation type="unfinished"></translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="60"/>
        <source>&amp;Swedish</source>
        <translation type="unfinished"></translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="65"/>
        <source>&amp;Icelandic</source>
        <translation type="unfinished"></translation>
    </message>
</context>
</TS>

mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>120</x>
      <y>80</y>
      <width>181</width>
      <height>17</height>
     </rect>
    </property>
    <property name="text">
     <string>Hello, world! :-)</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>400</width>
     <height>25</height>
    </rect>
   </property>
   <widget class="QMenu" name="menuFile">
    <property name="title">
     <string>File</string>
    </property>
    <addaction name="action_Swedish"/>
    <addaction name="action_Icelandic"/>
   </widget>
   <addaction name="menuFile"/>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
  <action name="action_Swedish">
   <property name="text">
    <string>&amp;Swedish</string>
   </property>
  </action>
  <action name="action_Icelandic">
   <property name="text">
    <string>&amp;Icelandic</string>
   </property>
  </action>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

推荐答案

好的,您甚至可以想象得到更简单的解决方案.添加:

Ok guys, solution is simpler that you can even imagine. Add:

QTranslator *translator;

进入 mainwindow.h

然后,在speakIcelandic()speakSwedish()(在 mainwindow.cpp 中)中,只需将实现代码更改为:

then, in speakIcelandic() and speakSwedish() (in mainwindow.cpp) just change the implementation code to:

bool ok = translator->load("se.qm");
qDebug("translation %d", ok);
qApp->installTranslator(translator);

ui->retranslateUi(this);

qDebug() << "Swedish";

别忘了在构造函数中添加translator = new QTranslator();,它终于可以工作了!

Dont forget to add translator = new QTranslator(); in constructor, and it will work, finally!

这篇关于Qt语言学家-设置应用程序Qt * .ui文件的转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆