📄 i18n-main-cpp.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Qt Toolkit - i18n/main.cpp example file</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: white; color: black; }--></style></head><body bgcolor="#ffffff"><table width="100%"><tr><td><a href="index.html"><img width="100" height="100" src="qtlogo.png"alt="Home" border="0"><img width="100"height="100" src="face.png" alt="Home" border="0"></a><td valign="top"><div align="right"><img src="dochead.png" width="472" height="27"><br><a href="classes.html"><b>Classes</b></a>- <a href="annotated.html">Annotated</a>- <a href="hierarchy.html">Tree</a>- <a href="functions.html">Functions</a>- <a href="index.html">Home</a>- <a href="topicals.html"><b>Structure</b> <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></a></div></table><h1 align=center>Internationalization</h1><br clear="all"> This example shows how to internationalize applications. Start it with <pre># i18n de</pre> to get a german version and with <pre># i18n en</pre> to get the english version. Also see <a href="i18n.html">the internationalization documentation</a>. <hr> Header file: <pre>/****************************************************************************** $Id: qt/examples/i18n/mywidget.h 2.3.8 edited 2004-05-12 $**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#ifndef MYWIDGET_H#define MYWIDGET_H#include <<a href="qmainwindow-h.html">qmainwindow.h</a>>#include <<a href="qstring-h.html">qstring.h</a>>class MyWidget : public QMainWindow{ Q_OBJECTpublic: MyWidget( <a href="qwidget.html">QWidget</a>* parent=0, const char* name = 0 );signals: void closed();protected: void closeEvent(<a href="qcloseevent.html">QCloseEvent</a>*);private: static void initChoices(<a href="qwidget.html">QWidget</a>* parent);};#endif</pre> <hr> Implementation: <pre>/****************************************************************************** $Id: qt/examples/i18n/mywidget.cpp 2.3.8 edited 2004-05-12 $**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#include <<a href="qbuttongroup-h.html">qbuttongroup.h</a>>#include <<a href="qradiobutton-h.html">qradiobutton.h</a>>#include <<a href="qlabel-h.html">qlabel.h</a>>#include <<a href="qlistbox-h.html">qlistbox.h</a>>#include <<a href="qcombobox-h.html">qcombobox.h</a>>#include <<a href="qlabel-h.html">qlabel.h</a>>#include <<a href="qhbox-h.html">qhbox.h</a>>#include <<a href="qvbox-h.html">qvbox.h</a>>#include <<a href="qaccel-h.html">qaccel.h</a>>#include <<a href="qpopupmenu-h.html">qpopupmenu.h</a>>#include <<a href="qmenubar-h.html">qmenubar.h</a>>#include <<a href="qstatusbar-h.html">qstatusbar.h</a>>#include <<a href="qapplication-h.html">qapplication.h</a>>#include "mywidget.h"MyWidget::MyWidget( <a href="qwidget.html">QWidget</a>* parent, const char* name ) : <a href="qmainwindow.html">QMainWindow</a>( parent, name ){ <a href="qvbox.html">QVBox</a>* central = new <a href="qvbox.html">QVBox</a>(this); central-><a href="qframe.html#b11d00">setMargin</a>( 5 ); central-><a href="qhbox.html#0075a0">setSpacing</a>( 5 ); <a href="qmainwindow.html#fce9ba">setCentralWidget</a>(central); <a href="qpopupmenu.html">QPopupMenu</a>* file = new <a href="qpopupmenu.html">QPopupMenu</a>(this); file-><a href="qmenudata.html#deddb9">insertItem</a>( <a href="qobject.html#2418a9">tr</a>("E&xit"), qApp, SLOT(quit()), <a href="qaccel.html#3ff091">QAccel::stringToKey</a>(<a href="qobject.html#2418a9">tr</a>("Ctrl+Q")) ); <a href="qmainwindow.html#0eb7bc">menuBar</a>()->insertItem( <a href="qobject.html#2418a9">tr</a>("&File"), file ); <a href="qwidget.html#d6a291">setCaption</a>( tr( "Internationalization Example" ) ); <a href="qstring.html">QString</a> l; <a href="qmainwindow.html#530937">statusBar</a>()->message( <a href="qobject.html#2418a9">tr</a>("Language: English") ); ( void )new <a href="qlabel.html">QLabel</a>( <a href="qobject.html#2418a9">tr</a>( "The Main Window" ), central ); <a href="qbuttongroup.html">QButtonGroup</a>* gbox = new <a href="qbuttongroup.html">QButtonGroup</a>( 1, QGroupBox::Horizontal, <a href="qobject.html#2418a9">tr</a>( "View" ), central ); (void)new <a href="qradiobutton.html">QRadioButton</a>( <a href="qobject.html#2418a9">tr</a>( "Perspective" ), gbox ); (void)new <a href="qradiobutton.html">QRadioButton</a>( <a href="qobject.html#2418a9">tr</a>( "Isometric" ), gbox ); (void)new <a href="qradiobutton.html">QRadioButton</a>( <a href="qobject.html#2418a9">tr</a>( "Oblique" ), gbox ); <a href=#86>initChoices</a>(central); }static const char* choices[] = { QT_TRANSLATE_NOOP( "MyWidget", "First" ), QT_TRANSLATE_NOOP( "MyWidget", "Second" ), QT_TRANSLATE_NOOP( "MyWidget", "Third" ), 0}; void <a name="86"></a>MyWidget::initChoices(<a href="qwidget.html">QWidget</a>* parent){ <a href="qlistbox.html">QListBox</a>* lb = new <a href="qlistbox.html">QListBox</a>( parent ); for ( int i = 0; choices[i]; i++ ) lb-><a href="qlistbox.html#04a671">insertItem</a>( <a href="qobject.html#2418a9">tr</a>( choices[i] ) ); }void <a name="87"></a>MyWidget::closeEvent(<a href="qcloseevent.html">QCloseEvent</a>* e){ <a href="qwidget.html#c04094">QWidget::closeEvent</a>(e); emit closed();}</pre> <hr> Main:<pre>/****************************************************************************** $Id: qt/examples/i18n/main.cpp 2.3.8 edited 2004-05-12 $**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#include <<a name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>>#include <<a name="qtranslator.h"></a><a href="qtranslator-h.html">qtranslator.h</a>>#include <<a name="qfileinfo.h"></a><a href="qfileinfo-h.html">qfileinfo.h</a>>#include <<a name="qmessagebox.h"></a><a href="qmessagebox-h.html">qmessagebox.h</a>>#include <<a name="qcheckbox.h"></a><a href="qcheckbox-h.html">qcheckbox.h</a>>#include <<a name="qvbox.h"></a><a href="qvbox-h.html">qvbox.h</a>>#include <<a name="qlayout.h"></a><a href="qlayout-h.html">qlayout.h</a>>#include <<a name="qbuttongroup.h"></a><a href="qbuttongroup-h.html">qbuttongroup.h</a>>#include <<a name="qpushbutton.h"></a><a href="qpushbutton-h.html">qpushbutton.h</a>>#include <<a name="qsignalmapper.h"></a><a href="qsignalmapper-h.html">qsignalmapper.h</a>>#include <stdlib.h>#if defined(_OS_UNIX_)#include <unistd.h>#endif#include "mywidget.h"//#define USE_I18N_FONTclass QVDialog : public QDialog {public: QVDialog(<a name="QWidget"></a><a href="qwidget.html">QWidget</a> *parent=0, const char *name=0, bool modal=FALSE, WFlags f=0) : <a name="QDialog"></a><a href="qdialog.html">QDialog</a>(parent,name,modal,f) { <a name="QVBoxLayout"></a><a href="qvboxlayout.html">QVBoxLayout</a>* vb = new <a href="qvboxlayout.html">QVBoxLayout</a>(this,8); vb-><a name="setAutoAdd"></a><a href="qlayout.html#a83e73">setAutoAdd</a>(TRUE); hb = 0; sm = new <a name="QSignalMapper"></a><a href="qsignalmapper.html">QSignalMapper</a>(this); connect(sm,SIGNAL(mapped(int)),this,SLOT(done(int))); } void addButtons( const QString& cancel=QString::null, const QString& ok=QString::null, const QString& mid1=QString::null, const QString& mid2=QString::null, const QString& mid3=QString::null) { addButton(ok.isNull() ? tr("OK") : ok, 1); if ( !mid1.isNull() ) addButton(mid1,2); if ( !mid2.isNull() ) addButton(mid2,3); if ( !mid3.isNull() ) addButton(mid3,4); addButton(cancel.isNull() ? tr("Cancel") : cancel, 0); } void addButton( const QString& text, int result ) { if ( !hb ) hb = new <a name="QHBox"></a><a href="qhbox.html">QHBox</a>(this); <a name="QPushButton"></a><a href="qpushbutton.html">QPushButton</a> *c = new <a href="qpushbutton.html">QPushButton</a>(text, hb); sm-><a name="setMapping"></a><a href="qsignalmapper.html#2531b8">setMapping</a>(c,result); connect(c,SIGNAL(clicked()),sm,SLOT(map())); }private: <a href="qsignalmapper.html">QSignalMapper</a> *sm; <a href="qhbox.html">QHBox</a> *hb;};MyWidget* showLang(<a name="QString"></a><a href="qstring.html">QString</a> lang){ static QTranslator *translator = 0; qApp->setPalette(<a name="QPalette"></a><a href="qpalette.html">QPalette</a>(<a name="QColor"></a><a href="qcolor.html">QColor</a>(220-rand()%64,220-rand()%64,220-rand()%64))); lang = "mywidget_" + lang + ".qm"; <a name="QFileInfo"></a><a href="qfileinfo.html">QFileInfo</a> fi( lang ); if ( !fi.<a name="exists"></a><a href="qfileinfo.html#a1625c">exists</a>() ) { <a name="QMessageBox::warning"></a><a href="qmessagebox.html#63edba">QMessageBox::warning</a>( 0, "File error", <a href="qstring.html">QString</a>("Cannot find translation for language: "+lang+ "\n(try eg. 'de', 'ko' or 'no')") ); return 0; } if ( translator ) { qApp->removeTranslator( translator ); delete translator; } translator = new <a name="QTranslator"></a><a href="qtranslator.html">QTranslator</a>( 0 ); translator->load( lang, "." ); qApp->installTranslator( translator ); MyWidget *m = new MyWidget; m-><a name="setCaption"></a><a href="qwidget.html#d6a291">setCaption</a>("Qt Example - i18n - " + m-><a name="caption"></a><a href="qwidget.html#f852bc">caption</a>() ); return m;}int main( int argc, char** argv ){ <a name="QApplication"></a><a href="qapplication.html">QApplication</a> app( argc, argv ); const char* qm[]= { "cs", "de", "el", "en", "eo", "fr", "it", "jp", "ko", "no", "ru", "zh", 0 };#if defined(_OS_UNIX_) srand(getpid()<<2);#endif //QFont font("i18n,unifont,cyberbit;helvetica",16,50,FALSE,QFont::Unicode); <a name="QFont"></a><a href="qfont.html">QFont</a> font("unifont",16,50,FALSE,QFont::Unicode); qApp->setFont(font); <a href="qstring.html">QString</a> lang; if ( argc == 2 ) lang = argv[1]; if ( argc != 2 || lang == "all" ) { QVDialog dlg(0,0,TRUE); <a name="QCheckBox"></a><a href="qcheckbox.html">QCheckBox</a>* qmb[sizeof(qm)/sizeof(qm[0])]; int r; if ( lang == "all" ) { r = 2; } else { <a name="QButtonGroup"></a><a href="qbuttongroup.html">QButtonGroup</a> *bg = new <a href="qbuttongroup.html">QButtonGroup</a>(4,Qt::Vertical,"Choose Locales",&dlg); for ( int i=0; qm[i]; i++ ) qmb[i] = new <a href="qcheckbox.html">QCheckBox</a>((const char*)qm[i],bg); dlg.addButtons("Cancel","OK","All"); r = dlg.<a name="exec"></a><a href="qdialog.html#0beb1a">exec</a>(); } if ( r ) { bool tight = qApp->desktop()->width() < 1024; int x=5; int y=25; for ( int i=0; qm[i]; i++ ) { if ( r == 2 || qmb[i]->isChecked() ) { MyWidget* w = showLang((const char*)qm[i]); if( w == 0 ) exit( 0 ); <a name="QObject::connect"></a><a href="qobject.html#7f8e37">QObject::connect</a>(w, SIGNAL(closed()), qApp, SLOT(quit())); w-><a name="setGeometry"></a><a href="qwidget.html#9ede68">setGeometry</a>(x,y,197,356); w-><a name="show"></a><a href="qmainwindow.html#eb53e3">show</a>(); if ( tight ) { x += 8; y += 8; } else { x += 205; if ( x > 1000 ) { x = 5; y += 384; } } } } } else { exit( 0 ); } } else { <a href="qstring.html">QString</a> lang = argv[1]; <a href="qwidget.html">QWidget</a>* m = showLang(lang); app.<a name="setMainWidget"></a><a href="qapplication.html#7ad759">setMainWidget</a>( m ); m-><a href="qwidget.html#d6a291">setCaption</a>("Qt Example - i18n"); m-><a name="show"></a><a href="qwidget.html#200ee5">show</a>(); } // While we run "all", kill them all#ifdef USE_I18N_FONT memorymanager->savePrerenderedFont(font.<a name="handle"></a><a href="qfont.html#fd3bbb">handle</a>(),FALSE);#endif return app.<a name="exec"></a><a href="qapplication.html#84c7bf">exec</a>();}</pre><p><address><hr><div align="center"><table width="100%" cellspacing="0" border="0"><tr><td>Copyright
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -