17.6 MINIMALIST GUI PROGRAMS IN QT


17.6 MINIMALIST GUI PROGRAMS IN QT

Every GUI program in Qt must have the following two features:

  • It must consist of exactly one object of type QApplication. This object initializes GUI features such as colors, fonts, cursor, and so on. This object is also in charge of all event handling, a topic we will discuss in a later section. The QApplication object must be brought into existence before any other graphical object is created.

  • Every GUI program in Qt must also specify one "main widget". Usually, the show() command will be invoked on this main widget, which would also automatically "show" the child widgets in the containment hierarchy of the main widget. When the main widget is closed, it causes the system to exit the GUI program. If a main widget has not been specified for a GUI program, closing the window on the terminal screen by, say, clicking on the "Close" button of the system-supplied window menu will not terminate the GUI program—it will continue to run in the background.

The following example displays a window whose width is 500 pixels and height 300 pixels and whose upper left-hand corner is located at pixel coordinates (200, 100) with respect to the upper left-hand corner of the terminal screen:

 
//FirstWindow.cc #include <qapplication.h> #include <qmainwindow.h> int main( int argc, char **argv ) //(A) { QApplication myApp( argc, argv ); //(B) QMainWindow* myWin = new QMainWindow( 0, 0, 0 ); //(C) myWin->resize( 500, 300 ); //(D) myWin->move( 200, 100 ); //(E) myApp.setMainWidget(myWin ); //(F) myWin->show(); //(G) return myApp.exec(); //(H) }

First note that main is written as if we want to invoke the program with command line arguments for the parameters argc and argv in line (A). Despite the syntax in line (A), the executable is called without command line arguments. The parameters argc and argv are provided because the compiler synthesizes arguments for these parameters that are then passed on to the QApplication constructor in line (B) where we create the mandatory QApplication object.

In line (C), we then invoke a constructor for QMainWindow. This creates a top-level window, with a menu bar, some tool bars and a status bar. To understand the arguments of the constructor in line (C), we need to show the prototype of the constructor:

 QMainWindow::QMainWindow( QWidget* parent = 0,                           const char* name = 0,                           WFlags f = WType_TopLevel ); 

The first parameter, parent, is used to establish a parent—child containment hierarchy of widgets for a graphical interface on the basis of what contains what.[11] In our simple example, we can set this to the null pointer, the symbol 0. The second parameter, name, is the nominal name of the QMainWindow object being created; this name can be used as a handle to the object. The last parameter, f, permits special flags to be designated for giving a custom look to the top-level window. For most applications of Qt, this parameter will also be set to 0.

Lines (D) and (E) establish the size and the position of the window on the user's terminal screen. Both are in pixels from the upper left-hand corner of the terminal screen. The first argument is for the horizontal dimension and the second for the vertical. As with Java, the horizontal coordinate increases to the right and the vertical coordinate from top to bottom.

Line (F) declares the QMainWindow object to be the main widget of the program. As was mentioned earlier, a widget is the main widget if its deletion would cause the application to be terminated.

To make the window just created visible on a terminal screen, we invoke its show() method in line (G). Finally, main passes the control to the exec() method of the QApplication object in line (H). As we will see later in further detail, invoking exec() causes the control to enter the event processing loop for user interaction. In our example, the control will stay in this loop until the displayed window is destroyed.

Assuming the above program was stored in a file called FirstWindow.cc, it can be compiled by the following command line[12]

   g++ -o FirstWindow FirstWindow.cc -I$QTDIR/include -L$QTDIR/lib -lqt 

where the environment variable QTDIR points to the directory containing the Qt software.

Figure 17.5 shows the window created by FirstWindow.cc.

click to expand
Figure 17.5

As written, the above program does not respond to any interaction with a human. Let's now consider another minimalist Qt program that shows a button in a window and, if you click on the button, actually causes the entire program to quit.

 
//FirstWindowWithButton.cc #include <qapplication.h> #include <qmainwindow.h> #include <qpushbutton.h> #include <qfont.h> int main( int argc, char **argv ) { QApplication myApp( argc, argv ); //(A) QMainWindow* myWin = new QMainWindow( 0, 0, 0 ); //(B) myWin->resize( 500, 300 ); //(C) myWin->move( 200, 100 ); //(D) QPushButton* quitButton = new QPushButton( "Quit", myWin ); //(E) quitButton->resize( 60, 30 ); //(F) quitButton->move( 220, 135 ); //(G) quitButton->setFont( QFont( "Times", 18, QFont::Bold ) ); //(H) QObject::connect( quitButton, SIGNAL(clicked()), &myApp, SLOT(quit()) ); //(I) myApp.setMainWidget( myWin ); //(J) myWin->show(); //(L) return myApp.exec(); //(M) }

Lines (A) through (D) are the same as in the previous program. In line (E), we create a pushbutton object and, via the second argument of the QPushButton constructor, we cause the pushbutton object to be a child widget of the parent QMainWindow widget constructed in line (B). The pixel coordinates for sizing and positioning as specified in the lines (F) and (G) are with respect to the upper left-hand corner of the parent widget myWin, whereas those specified for the parent widget in lines (C) and (D) are with respect to the upper left-hand corner of the terminal window.

It is the code in line (I) that tells the program how to respond to a click on the button. As we will explain in greater detail in Section 17.14, this statement connects the signal produced by the button when it is clicked with the slot quit() of the QApplication object myApp.

Figure 17.6 shows the window created by the above program.

click to expand
Figure 17.6

[11]In a class hierarchy in an OO program, it is not uncommon to refer to a base class as a parent and a derived class as a child. But that is not what is meant by a parent-child relationship between the widgets of a GUI program. In a GUI program, a widget B is a child of a parent widget A if A is serving as a "container" for B.

[12]This command line invocation may not work if the compiler cannot locate the Xll libraries in your machine. As a case in point, with the Mandrake packaging of Linux, you have to add/usr/XllR6/lib to the file ld.so.conf in the/etc directory and run the ldconfig command before the command-line invocation of the compiler shown here will work. Another option, the only option if you do not have root access, would be to add the path /usr/X11R6/lib to the user-defined environment variable LD_LIBRARY_PATH.




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net