最近在学习qt国际化这一部分,现在有个问题就是动态切换。当我点击某个按钮之后,程序能够将所有需要翻译的字符串重新翻译,我现在谢了一个特别小的程序,但不知道为什么就是不翻译?求解。谢谢!!!
1条回答 默认 最新
- 「已注销」 2015-11-17 01:25关注
这个是代码:
//一个按钮****
#include "Dialog.h"
Dialog::Dialog(void)
{
button=new QPushButton(this);
updateUi();
}Dialog::~Dialog(void)
{
}void Dialog::updateUi()
{
button->setText(tr("change"));
this->setWindowTitle(tr("Dialog"));
}void Dialog::Pushbutton_clicked()
{
emit SwitchLanguaged();
}void Dialog::SwitchLanguaged()
{}
void Dialog::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch(e->type())
{
case QEvent::LanguageChange:
updateUi();
break;
default:
break;
}
}#pragma once
#include
#include
#includeclass Dialog:public QDialog
{
public:
Dialog(void);
~Dialog(void);
void changeEvent(QEvent *);
void updateUi();public:
QPushButton *button;
signals:
void SwitchLanguaged();public slots:
//void SwitchLanguaged();
void Pushbutton_clicked();
};# //主界面
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "mainwindow.h"
#include "Dialog.h"
MainWindow::MainWindow()
{
QWidget *widget=new QWidget;
setCentralWidget(widget);//设置中央WidgetinfoLabel=new QLabel("Choose a menu option,or right-click to invoke action");
infoLabel->setAlignment(Qt::AlignCenter);//居中对齐
QVBoxLayout *layout=new QVBoxLayout;
layout->addWidget(infoLabel);widget->setLayout(layout);
createActions();
createMenus();QString msg="popup a menu by right-clicking.";
statusBar()->showMessage(msg);setMinimumSize(160,160);//设置最小尺寸
resize(480,320);//设置尺寸Dialog *d=new Dialog;
connect(d,SIGNAL(SwitchLanguaged()),this,SLOT(LanguageChanged()));
updateUi();}
//创建Actions
void MainWindow::createActions()
{
newAct=new QAction("&New",this);
newAct->setShortcut(QKeySequence("Ctrl+N"));//设置快捷键
newAct->setStatusTip("New a file");//设置状态栏提示
connect(newAct,SIGNAL(triggered()),this,SLOT(newFile()));openAct = new QAction("&Open...", this); openAct->setShortcut(tr("Ctrl+O")); openAct->setStatusTip("Open an existing file"); connect(openAct, SIGNAL(triggered()), this, SLOT(openFile())); saveAct = new QAction("&Save", this); saveAct->setShortcut(tr("Ctrl+S")); saveAct->setStatusTip("save the file"); connect(saveAct, SIGNAL(triggered()), this, SLOT(saveFile())); printAct = new QAction("&Print...", this); printAct->setShortcut(tr("Ctrl+P")); printAct->setStatusTip("Print the file"); connect(printAct, SIGNAL(triggered()), this, SLOT(printFile())); exitAct = new QAction("E&xit", this); exitAct->setShortcut(tr("Ctrl+Q")); exitAct->setStatusTip("Exit the Application."); connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); boldAct = new QAction(tr("&Bold"), this); boldAct->setCheckable(true);//可选择 boldAct->setShortcut(tr("Ctrl+B")); boldAct->setStatusTip(tr("Make the text bold")); connect(boldAct, SIGNAL(triggered()), this, SLOT(bold())); QFont boldFont = boldAct->font(); boldFont.setBold(true);//字体加粗 boldAct->setFont(boldFont);//设置字体 italicAct = new QAction(tr("&Italic"), this); italicAct->setCheckable(true); italicAct->setShortcut(tr("Ctrl+I")); italicAct->setStatusTip(tr("Make the text italic")); connect(italicAct, SIGNAL(triggered()), this, SLOT(italic())); QFont italicFont = italicAct->font(); italicFont.setItalic(true);//设置倾斜 italicAct->setFont(italicFont); aboutAct=new QAction("&About",this); aboutAct->setStatusTip("About the App"); connect(aboutAct,SIGNAL(triggered()),this,SLOT(about()));
}
//创建菜单
void MainWindow::createMenus()
{
//文件菜单
fileMenu=menuBar()->addMenu("&File");
fileMenu->addAction(newAct);
fileMenu->addAction(openAct);
fileMenu->addAction(saveAct);
fileMenu->addAction(printAct);
fileMenu->addSeparator();
fileMenu->addAction(exitAct);//编辑菜单
editMenu=menuBar()->addMenu("&Edit");
formatMenu=editMenu->addMenu("&Format");
formatMenu->addAction(boldAct);
formatMenu->addAction(italicAct);//帮助菜单
helpMenu=menuBar()->addMenu("&Help");
helpMenu->addAction(aboutAct);//翻译
//tranMenu=menuBar()->addMenu(tr("Tran"));//connect(
}
void MainWindow::newFile()
{
infoLabel->setText("InvokeFile|New");
}
void MainWindow::openFile()
{
infoLabel->setText("InvokeFile|Open");
}
void MainWindow::saveFile()
{
infoLabel->setText("InvokeFile|Save");
}void MainWindow::printFile()
{
infoLabel->setText("InvokeFile|Print");
}//关于对话框
void MainWindow::about()
{
infoLabel->setText("InvokeHelp|About");
QMessageBox::about(this,"About","How to createMenus");
}void MainWindow::bold()
{
infoLabel->setText("InvokeEdit|Format|bold");
}void MainWindow::italic()
{
infoLabel->setText("InvokeEdit|Format|italic");
}
//自定义部分
void MainWindow::LanguageChanged()
{
tran->load("cn.qm");
qApp->installTranslator(tran);
updateUi();}
void MainWindow::updateUi()
{
setWindowTitle(tr("Menus"));
}
void MainWindow::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch(e->type())
{
case QEvent::LanguageChange:
updateUi();
break;
default:
break;
}
}#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include
#include
#include
#include
class QMenu;
class QAction;
class QLabel;class MainWindow:public QMainWindow
{
Q_OBJECT
public:
MainWindow();
void changeEvent(QEvent *);
void updateUi();
public:
QTranslator *tran;//翻译文件private slots:
void newFile();
void openFile();
void saveFile();
void printFile();
void about();
void bold();
void italic();
void LanguageChanged();
private:
QMenu* fileMenu;//文件菜单
QMenu* editMenu;//编辑菜单
QMenu* helpMenu;//帮助菜单
//QMenu* tranMenu;QMenu* formatMenu;
QAction* newAct;//新建
QAction* openAct;//打开
QAction* saveAct;//保存
QAction* printAct;//打印
QAction* exitAct;//退出
QAction* boldAct;
QAction* italicAct;QAction* aboutAct;//关于
QLabel* infoLabel;void createActions();
void createMenus();};
#endif // MAINWINDOW_H
解决 无用评论 打赏 举报
悬赏问题
- ¥15 没输出运行不了什么问题
- ¥20 输入import torch显示Intel MKL FATAL ERROR,系统驱动1%,: Cannot load mkl_intel_thread.dll.
- ¥15 点云密度大则包围盒小
- ¥15 nginx使用nfs进行服务器的数据共享
- ¥15 C#i编程中so-ir-192编码的字符集转码UTF8问题
- ¥15 51嵌入式入门按键小项目
- ¥30 海外项目,如何降低Google Map接口费用?
- ¥15 fluentmeshing
- ¥15 手机/平板的浏览器里如何实现类似荧光笔的效果
- ¥15 盘古气象大模型调用(python)