1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
<!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></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.2 edited 2001-01-26 $
**
** 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_OBJECT
public:
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.2 edited 2001-01-26 $
**
** 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#0076cb">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=#189>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="189"></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="190"></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.2 edited 2001-06-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_FONT
class 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#5e9ab1">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 2001 Trolltech<td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align="right"><div align="right">Qt version 2.3.2</div>
</table></div></address></body></html>
|