xref: /netbsd-src/external/gpl2/gettext/dist/gettext-tools/examples/hello-c++-qt/hello.cc (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 // Example for use of GNU gettext.
2 // Copyright (C) 2003 Free Software Foundation, Inc.
3 // This file is in the public domain.
4 
5 // Source code of the C++ program.
6 
7 #include <qapplication.h>
8 #include <qmainwindow.h>
9 #include <qlabel.h>
10 #include <qpushbutton.h>
11 #include <qstring.h>
12 #include <qvbox.h>
13 #include <qhbox.h>
14 #include <qtextcodec.h>
15 
16 /* Get getpid() declaration.  */
17 #if HAVE_UNISTD_H
18 # include <unistd.h>
19 #endif
20 
21 int
main(int argc,char * argv[])22 main (int argc, char *argv[])
23 {
24   // Initializations.
25 
26   QApplication application (argc, argv);
27 #if 0
28   GettextTranslator *translator =
29     new GettextTranslator (&application, "hello-c++-qt", LOCALEDIR);
30 #else
31   QTranslator *translator = new QTranslator (NULL);
32   translator->load (QString ("hello-c++-qt") + "_" + QTextCodec::locale(),
33                     PKGLOCALEDIR);
34 #endif
35   application.installTranslator (translator);
36 #define _(string) application.translate ("", string)
37 
38   // Create the GUI elements.
39 
40   QMainWindow *window = new QMainWindow ();
41   window->setCaption ("Hello example");
42 
43   QVBox *panel = new QVBox (window);
44   panel->setSpacing (2);
45 
46   QLabel *label1 = new QLabel (_("Hello, world!"), panel);
47 
48   QString label2text;
49   // NOT using QString::sprintf because it doesn't support reordering of
50   // arguments.
51   //label2text.sprintf (_("This program is running as process number %d"),
52   //                    getpid ());
53   label2text = _("This program is running as process number %1.").arg(getpid ());
54   QLabel *label2 = new QLabel (label2text, panel);
55 
56   QHBox *buttonbar = new QHBox (panel);
57   QWidget *filler = new QWidget (buttonbar); // makes the button right-aligned
58   QPushButton *button = new QPushButton ("OK", buttonbar);
59   button->setMaximumWidth (button->sizeHint().width() + 20);
60   QObject::connect (button, SIGNAL (clicked ()), &application, SLOT (quit ()));
61 
62   panel->resize (panel->sizeHint ());
63   window->resize (panel->frameSize ());
64 
65   application.setMainWidget (window);
66 
67   // Make the GUI elements visible.
68 
69   window->show ();
70 
71   // Start the event loop.
72 
73   return application.exec ();
74 }
75