Gnome and KDE Programming -...

42
Gnome and KDE Programming 1

Transcript of Gnome and KDE Programming -...

Gnome and KDEProgramming

1

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Chapter 43Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Chapter 43

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:45 PM

Color profile: Generic CMYK printer profileComposite Default screen

Development support for application interfaces was an integral part of both theGnome (GNU Network Object Model Environment) and KDE (K DesktopEnvironment) projects from their inception. Any application can be designed

easily to use either Gnome or KDE graphical components such as windows, toolbars,and buttons. Their aim is to provide not just a consistent interface, but also a flexibleplatform for the development of powerful applications. Both are completely free underthe GNU Public License, with no restrictions.

Both Gnome and KDE provide extensive development libraries that contain functionsand definitions you can use in C and C++ programs to create Gnome or KDE interfacesfor your applications. You can code these directly into your program source code, or usea GUI development tool like KDevelop or Glade to construct your interfaces by simplyselecting and combining different components. This chapter will briefly describeessential aspects of both Gnome and KDE development. For more information anddetailed documentation, check the Gnome development site at developer.gnome.org,and the KDE development site at developer.kde.org. In addition, numerous open sourceprojects for both Gnome and KDE are under development at sourceforge.net. You cancheck here to download and examine source code for the kind of projects you may beinterested in. You can even start your own project there. Table 1 lists several Gnome andKDE development sites resources.

2 R e d H a t L i n u x : T h e C o m p l e t e R e f e r e n c e

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

Site Description

developer.gnome.org Gnome development site, including detaileddocumentation

glade.gnome.org Glade, the GTK+ User Interface Builder

developer.kde.org KDE development site, including detaileddocumentation

www.kdevelop.org KDevelop Integrated Development Environment(IDE) for KDE applications

www.trolltech.com QT site with documentation for QT libraries usedin KDE, as well as for QT Designer

www.sourceforge.net Open source development site for many Gnomeand KDE projects

Table 1. Gnome and KDE Development Resources

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:45 PM

Color profile: Generic CMYK printer profileComposite Default screen

Gnome Programming: GladeGnome provides libraries of Gnome GUI tools that developers can use to create Gnomeapplications, and programs can be said to be Gnome compliant if they use buttons,menus, and windows that adhere to a Gnome standard. Gnome applications makeuse of Gnome, GTK Toolkit, GGTK Drawing Kit, and GNU libraries. For detaileddescriptions of the functions, definitions, and structures contained in these libraries, itis strongly recommended that you use the extensive documentation available on theGnome developer’s Web site at developer.gnome.org. The Documentation sectionincludes detailed tutorials, manuals, and reference works, including the completereference for the Gnome, GTK, and GDK APIs.

GTK+ is the widget set used for Gnome applications, and its look and feel wasoriginally derived from Motif. A widget set is the set of GUI objects that are availablefor use in a desktop. Buttons, windows, and toolbars are all examples of widgets. Thewidget set is designed from the ground up for power and flexibility. For example,buttons can have labels, images, or any combination thereof. Objects can be dynamicallyqueried and modified at run time. GTK+ also includes a theme engine that lets userschange the look and feel of applications using these widgets. At the same time, theGTK+ widget set remains small and efficient.

The GTK+ widget set is entirely free under the Library General Public License(LGPL). The LGPL allows developers to use the widget set with proprietary as well asfree software. The widget set also features an extensive set of programming languagebindings including C++, Perl, Python, Pascal, Objective-C, Guile, and Ada. Internalizationis fully supported, permitting applications based on GTK+ to be used with othercharacter sets, such as those of Asian languages. The drag-and-drop functionalitysupports both XDND and Motif protocols, allowing drag-and-drop operations withother widget sets that support these protocols, such as Qt and Motif.

Programs written to work on Gnome are essentially C programs that containGnome and GTK+ functions. The Gnome and GTK+ functions handle the Gnomedesktop operations for a program. When programming for Gnome, you will make useof a very extensive set of functions and structures contained in many libraries—thesefunctions and structures make up the different components that go into a Gnomeapplication.

This chapter can only provide a general overview of these libraries and of howyou use them to create Gnome programs. Though Gnome is not as easy to use as Tk,programming in Gnome requires the use of only a few basic functions to create simpleuser interfaces. You can think of GTK+ functions as lower-level operations and Gnomefunctions as easy-to-use higher-level operations. The Gnome functions usually incorporateseveral GTK+ functions, making GUI tasks relatively easy to program. A Gnomeprogram is essentially a C program with GTK+ functions as well as Gnome functions.Because several basic Gnome operations are handled by GTK+ functions, this chapterbegins by discussing basic GTK+ programming, and then discusses Gnome programs.

G n o m e a n d K D E P r o g r a m m i n g 3

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:45 PM

Color profile: Generic CMYK printer profileComposite Default screen

The Gnome libraries provide the highest-level functions used in Gnome applications.Below them are the GTK+ libraries. GTK+ is the toolkit developed for the GNU ImageManipulation Program (GIMP). GTK+ is made up of the GIMP Toolkit (GTK) and GIMPDrawing Kit (GDK) libraries.

GTK contains the functions and structures for managing widgets and user interfacetasks. These functions and structures can be accessed directly in any Gnome program.In fact, a Gnome application is a GTK program with Gnome library functions. GTKfunctions and structures are C++ program objects, both designed to be used in a C++style program.

GDK contains lower-level functions that are used to connect GTK to the Xliblibraries. The Xlib libraries hold functions that perform the actual X Window Systemoperations. Both GTK and Gnome also make use of the standard C functions providedby the Glib library. Table 2 lists the different Gnome components.

Gnome applications also make use of ORBit and Imlib (Image Library). Gnomeuses the Common Object Request Broker Architecture (CORBA), which allows softwarecomponents to interconnect, regardless of the computer language in which they areimplemented or the kind of machine they are running on. The Gnome implementationof CORBA is called ORBit. With ORBit, programs can locate and request services from anobject, even one located across a network. For example, an editor could request the use ofa spreadsheet. Imlib contains functions for managing images in various formats, lettingyou display, scale, save, and load images into your program.

Gnome LibrariesThe Gnome libraries make it possible for Gnome applications to have the same kind ofGUI interface with the same look and feel. Though a Gnome application is a GTKprogram with Gnome library functions, the Gnome library provides several complexhigher-level widgets, as well as many simple operations not included in the GTK+libraries. Table 3 lists the Gnome libraries.

4 R e d H a t L i n u x : T h e C o m p l e t e R e f e r e n c e

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

Gnome Component Description

Gnome libraries Contains high-level Gnome functions

GTK (GIMP Tool Kit) Contains widgets and GUI functions

GDK (GIMP Drawing Kit) Provides a low-level wrapper for Xlib

Xlib Provides X Window operations

Glib Contains the GNU C library of standardfunctions

Table 2. Gnome Components

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:45 PM

Color profile: Generic CMYK printer profileComposite Default screen

G n o m e a n d K D E P r o g r a m m i n g 5

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

Libgnome and libgnomeui are the two main libraries needed for any Gnomeapplications. Libgnome is a set of functions designed to be independent of any particularGUI toolkit. These functions could be used in any kind of program, whether it be onewith just a command line interface or even no interface. These functions are independentof any particular GUI toolkit. The libgnomeui library contains functions that provideGUI interface operations. These are tied to a particular GUI toolset, such as the GTK. It ispossible to create a libgnomeui library that is tied to a different GUI toolset.

The libgnome library provides many utility routines related to the Gnome desktopenvironment. Among the capabilities provided are config file support for applicationsto store persistent data, support for metadata (data attached to file objects, like the icon

Library Description

libaudiofile Reads a wide variety of audio file formats (AIFF, AIFC,WAV, and NeXT/Sun au).

libgdk_imlib Includes functions to load multiple file formats (JPEG,GIF, TIFF, PNG, XPM, PPM, PGM, PBM, and BMP).

libgtk This is the GTL Toolkit library. Gnome applicationsare written entirely using libgtk for all GUI elements(buttons, menus, scroll bars, and so on).

libgnome Includes utility routines for the Gnome desktopenvironment, such as routines for configuration, help,managing mime types, and managing sessions. Thislibrary is independent of any GUI toolkit.

libgnomeui Includes toolkit extensions to the GTK+ widget set forcreating dialog boxes and message boxes, menu bars,toolbars, status lines, and so on. It also includes iconsfor use in dialog boxes, menu entries, and buttons, andit provides the Gnome canvas for the easy creation ofcomplex interfaces, such as address books, calendarapplications, and spreadsheets. This is a toolkit-dependent library currently using the GTK+ Toolkit.

libgnorba A library for using the ORBit CORBA implementationwith Gnome.

libzvt A library containing a terminal widget.

libart_lgpl Contains graphic functions used for GnomeCanvas.

Table 3. Gnome Libraries

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:46 PM

Color profile: Generic CMYK printer profileComposite Default screen

that will display for a particular file type), and support for loading help documentsinto the Gnome help browser. An interface is also provided so Gnome applicationscan talk to the Gnome session manager. Finally, there are routines to configure howdifferent mime types are handled by Gnome and the Gnome file manager. Some ofthese functions are listed in Table 4.

6 R e d H a t L i n u x : T h e C o m p l e t e R e f e r e n c e

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

Gnome Library Function Category Description

gnome-config Provides simple access toconfiguration values.

gnome-defs Contains Gnome definitions for C++linking.

gnome-exec Permits execution of programs fromGnome applications.

gnome-help Contains routines for displaying help.

gnome-history Keeps track of recently useddocuments.

gnome-i18n Provides support for localization andinternationalization.

gnome-mime-info Contains routines to get informationbound to a MIME type.

gnome-paper Contains paper dimensions andprinting unit conversions.

gnome-popt Contains the command line argumentparser.

gnome-regex Contains the regular expression cacheimplementation.

gnome-sound Includes sound-playing routines forGnome applications.

gnome-triggers Contains a hierarchical signalmechanism for application events.

gnome-url Permits launching viewers fordocuments based on their URL.

gnome_lib Initializes libgnome library.

Table 4. Gnome Library (libgnome)

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:46 PM

Color profile: Generic CMYK printer profileComposite Default screen

The libgnomeui library contains the functions and structures you need to createGnome user interfaces for your applications, and these functions are tied to the GTK+Toolkit. This library contains toolkit extensions to the GTK+ widget set, and programmerscan easily create dialog boxes and message boxes, as well as menu bars, toolbars, andstatus lines. An extensive array of stock icons is provided for programmers to use indialog boxes, menu entries, and buttons, and because all Gnome applications will uselibgnomeui to create these common GUI elements, visual consistency is guaranteed.Similar in many ways to the Tk canvas, the Gnome canvas provides a framework forcreating address books, calendar applications, and spreadsheets.

GUI applications require extensive use of images to create a friendly and comfortableuser interface. Traditionally, it has been difficult to load all of the common graphic fileformats into X11 applications. The libgdk_imlib library addresses this issue by providingconvenient and powerful functions to load multiple file formats (JPEG, GIF, TIFF, PNG,XPM, PPM, PGM, PBM, and BMP). These files are converted to an internal 24-bit RGBrepresentation, and utility functions exist to scale as well as render from 24-bit RGB toa variety of other color depths (with dithering if desired). Input image files are cachedinternally by libgdk_imlib to improve performance in applications that repeatedlyuse images.

The libgtk library is the GTK Toolkit library. It is a professional-quality widget setthat in many ways is superior to other widget sets. Gnome applications are writtenentirely using libgtk for all GUI elements (buttons, menus, scroll bars, and so on). Thelibgnorba library provides support for CORBA operations, such as obtaining referencesto objects and requesting new instances of objects. Libzvt is a simple library containinga terminal widget. Libart_lgpl holds graphic functions that can be used with theGnomeCanvas widget.

GTK+GTK+ consists of an extensive set of functions for widgets of various types, such asmenus, buttons, and windows. It also supports bindings that associate GUI events,such as mouse clicks, with objects, such as buttons. Check the online documentation forthe GTK API at the www.gtk.org and developer.gnome.org Web sites. The documentationincludes a comprehensive listing of all GTK functions, as well as a detailed tutorial onGTK programming. It is highly recommended that you make use of this documentation—due to size constraints, this book can only present brief introductions and list several ofthe common GTK functions. Also, check the GTK header files for a detailed declarationof different functions and structures, including their arguments and return values.

Several basic functions and components are needed in any GTK program. You firstneed to include at least the gtk.h header file. Other GTK header files may be required,depending on the widgets and functions you are using. You then have to define pointersto the widgets you intend to define and use. Then you have to initialize the GTK librarywith the gtk_init function. Once that’s done, you can define your widgets using GTKfunctions and assign their addresses to the pointers defined earlier. Then, you can use

G n o m e a n d K D E P r o g r a m m i n g 7

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:46 PM

Color profile: Generic CMYK printer profileComposite Default screen

8 R e d H a t L i n u x : T h e C o m p l e t e R e f e r e n c e

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

GTK functions to specify actions and attributes for the widgets, such as displayingthem. For example, a close box event (delete_event) is connected to the window andthe gtk_main_quit function so that when a user clicks the Close box of the window,the program ends. Finally, you use the gtk_main function to run the widgets.

The following base.c program defines a simple GTK program that displays asimple window:

#include <gtk/gtk.h>

int main( int argc, char *argv[] )

{

GtkWidget *window1;

gtk_init (&argc, &argv);

window1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);

gtk_widget_show (window1);

gtk_main ();

return(0);

}

The gtk.h header file includes GTK variable, macro, and function definitions.window1 is defined as a pointer to a structure named GtkWidget. The actual structurepointed to will later be determined by the function used to create a given structure. Thegtk_init function creates initial settings, such as the default visual and color map, andit then calls the gdk_init function to initialize the GTK library and check for GTKarguments. The gtk_window_new function creates a new window structure, returningits address, which is then assigned to the window pointer. The window is now pointingto the GTK window structure. The GTK_WINDOW_TOP_LEVEL argument will placethe window under the control of the window managers, using its window manager’sdefaults for displaying a window. The gtk_widget_show function then displays thewindow—notice that the window pointer is used as the argument to this function.Finally, the gtk_main function starts the interactive process, waiting for events tooccur, such as button selections and mouse clicks.

You compile a GTK+ program using the gcc compiler and the GTK+ libraries.To specify the GTK+ libraries on the command line, you use the gtk-config

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:46 PM

Color profile: Generic CMYK printer profileComposite Default screen

command. This command determines the compiler options needed to compile aGTK+ program.

gtk-config --cflags --libs

gtk-config is a command that needs to be executed on the command line. To do this,you surround it and its arguments with back quotes. Back quotes are shell operators thatare used to execute an enclosed command on the command line and place its returnedvalues in the same place on that line. You can think of this operation as functioningsomewhat like a macro, substituting returned values for the command executed. In thiscase, the gtk-config command with the cflags and libs arguments will place thecompiler GTK flags and libraries you need on the command line for the gcc command.The gcc command is then executed with those flags and libraries:

gcc hello.c –o hello `gtk-config --cflags --libs`

The libraries usually used are listed in Table 5.The program language types used in GTK+ programming can be categorized into

fundamental, built-in, and object types. The fundamental types are basic types, such asstandard C program types and the base class types for GTK+, like GTK_TYPE_OBJECT.The fundamental types are automatically defined by gtk_init. The built-in types includesome basic enumerations, flags, and structures like GdkWindow—these are types thatGTK+ need not understand to use. Object types consist of registered GtkObject types.

G n o m e a n d K D E P r o g r a m m i n g 9

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

Library Description

GTK (-lgtk) GTK widget library

GDK (-lgdk) Xlib wrapper

gmodule (-lgmodule) Runtime extensions

Glib (-lglib) GTK is built on top of Glib and always requires it

Xlib (-lX11) Used by GDK

Xext (-lXext) Shared memory pixmaps and other X extensions

math (-lm) Math library

Table 5. Commonly Used GTK Libraries

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:46 PM

Color profile: Generic CMYK printer profileComposite Default screen

10 R e d H a t L i n u x : T h e C o m p l e t e R e f e r e n c e

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

Signal and EventsGnome programming works like other GUI programming—it is event oriented. Inevent-driven programs, you first define the objects that the user can operate on, and thenyou start the interaction function that continually checks for certain events, such as mouseclicks and menu selections. When such an event is detected, it is passed to its appropriatefunction for handling. For example, if a user clicks on an OK button, the mouse click isdetected and control is passed to a function set up to handle a click on an OK button. Whenthe function is finished, it returns control back to the interaction program.

GTK adds a further level of sophistication. When events occur on a certain widget,the widget will emit a signal that is then used to execute a function associated bothwith that signal and that object. For example, when you click on a Close button, theClose button widget detects the mouse-click event and emits a “clicked” signal. Thesignal is detected and its associated function is executed.

You can also, if you wish, associate an event directly with a function. For this to work,the programmer has to connect a signal on a given object with a particular function.Functions associated with a particular signal are commonly referred to as “handlers” or“callbacks.” When a signal is emitted, its handlers or callbacks are invoked. This processis referred to as “emission.” Note that the signals referred to here are in no way like thesignals used in Unix systems.

To associate a particular event with the function you want executed for a givensignal, you use either the gtk_signal_connect or gtk_signal_connect_object functions.When the signal is detected, its associated function is automatically executed. Thegtk_signal_connect function is used for calling functions to which you may be passingarguments, and either gtk_signal_connect or gtk_signal_connect_object is used forcalling functions that require no arguments. In the following gtk_signal_connectsyntax statement, the object is the GtkObject you defined, such as a button. The nameis the name of the signal, such as a mouse click; func is the function you want executedwhenever an event for this object occurs; and func_data are any arguments beingpassed to that function.

gint gtk_signal_connect( GtkObject *object, gchar *name,

GtkSignalFunc func, gpointer func_data );

When a signal is detected for the specified object, its associated callback function iscalled and executed, as shown in this syntax statement:

void callback_func ( GtkWidget *widget, gpointer callback_data );

Therefore, to associate a click on a button with the hello function, you would usethe following gtk_signal_connect statement:

gtk_signal_connect (GTK_OBJECT (mybutton), "clicked",

GTK_SIGNAL_FUNC (hello), NULL);

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:47 PM

Color profile: Generic CMYK printer profileComposite Default screen

The object is mybutton, clicked is the click signal, and hello is a function theprogrammer wrote to be executed when this signal is detected. GTK_OBJECT andGTK_SIGNAL_FUNC are macros that perform type checking and casting to make surethe objects are passed with the appropriate types.

Certain objects have signals that can be associated with them. For example, thebutton object can be associated with a clicked signal or an enter signal. The clickedsignal occurs when a user presses down and then releases the mouse button, whereasan enter signal occurs when the user moves the mouse pointer over the button object.The button signals are the following:

■ pressed Mouse button pressed down when pointer positioned on the button

■ released Mouse button released when pointer positioned on the button

■ clicked Mouse button pressed down and released when pointer positionedon the button

■ enter Mouse pointer is moved onto the button

■ leave Mouse pointer is moved out of the button

You can also use the signal connection functions to connect events directly to anobject and function, instead of using signals. Events are messages transmitted by theX11 server to indicate occurrences like mouse clicks and menu selections. In thegtk_signal_connect function, you use the name of the event instead of the signal.Callback functions for events include an added argument for the event. The type forthis parameter can be GdkEvent or one of several other event types. These are listedin Table 6.

void callback_func( GtkWidget *widget, GdkEvent *event,

gpointer callback_data );

G n o m e a n d K D E P r o g r a m m i n g 11

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

Event Type GtkWidget Signal

GDK_DELETE "delete_event"

GDK_DESTROY "destroy_event"

GDK_EXPOSE "expose_event"

GDK_MOTION_NOTIFY "motion_notify_event"

GDK_BUTTON_PRESS "button_press_event"

Table 6. Commonly Used GTK Events

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:47 PM

Color profile: Generic CMYK printer profileComposite Default screen

12 R e d H a t L i n u x : T h e C o m p l e t e R e f e r e n c e

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

For example, to associate a button_press_event with an OK button, you woulduse “button_press_event” as the signal name. The following example associates abutton_press_event event on a button with the button_press_callback function:

gtk_signal_connect( GTK_OBJECT(button), "button_press_event",

GTK_SIGNAL_FUNC(button_press_callback), NULL);

The callback function used for the signal connection, in this casebutton_press_callback, would have the event type GdkEventButton for its eventargument.

static gint button_press_callback( GtkWidget *widget,

GdkEventButton *event, gpointer data );

The following example associates a click on a window Close box with the close-winfunction. The object is mywindow, delete_event is the Close-box event, and close-winis a function the programmer wrote with code to be executed when this event occurs.When a user clicks on the window’s Close box, the close-win function is called.

gtk_signal_connect (GTK_OBJECT (mywindow), "delete_event",

GTK_SIGNAL_FUNC (close-win), NULL);

Signals are stored in a global table. You can create your own signals with thegtk_signal_new function, and then use gtk_signal_emit to have an object emit a

Event Type GtkWidget Signal

GDK_2BUTTON_PRESS "button_press_event"

GDK_3BUTTON_PRESS "button_press_event"

GDK_BUTTON_RELEASE "button_release_event"

GDK_KEY_PRESS "key_press_event"

GDK_KEY_RELEASE "key_release_event"

GDK_FOCUS_CHANGE "focus_in_event", "focus_out_event"

GDK_SELECTION_CLEAR "selection_clear_event"

GDK_SELECTION_REQUEST "selection_request_event"

Table 6. Commonly Used GTK Events (continued)

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:47 PM

Color profile: Generic CMYK printer profileComposite Default screen

signal. gtk_signal_new will return an identifier for the new signal. You can use thiswith gtk_signal_emit to have your object emit that signal.

Gnome FunctionsGnome programs build on GTK+ programs providing Gnome functions to let youmore easily create Gnome interfaces that are consistent with the style for the Gnomedesktop. To create a simple GTK program, you begin with GTK object definitions foryour Gnome widgets and then use Gnome functions to initialize your program anddefine your widgets. GTK functions such as gtk_signal_connect are used to associateGUI events with objects, whereas Gnome functions such as gnome_app_create_menuscreate menus. In a Gnome program you need to include an initialization function calledgnome_init, which you place at the very beginning. To create a primary window foryour application, you use gnome_app_new.

The following example shows the use of the gnome_init and the gnome_app_newfunctions. The gnome_init function takes as its arguments any initial arguments thatthe user must enter when the program starts, as well as an application ID and versionnumber. The user’s initial arguments are managed by the argc and argv special variables.gnome_app_new takes as its arguments the title you want displayed in the applicationwindow and the name of the application object. It returns the address of the new objectwhich, in this example, is assigned to the app pointer. app is a pointer to an object oftype GtkWidget.

GtkWidget *app;

gnome_init ("", "0.1", argc, argv);

app = gnome_app_new ("Hello-World", "Hello App");

Other operations, such as displaying widgets and starting the interactive interface,are handled by GTK functions. gtk_widget_show_all will display a widget and anyother widgets it contains. gtk_main will start the interactive operations, detecting GUIevents such as mouse clicks and key presses and executing their associated functions.

gtk_widget_show_all(app);

gtk_main ();

Compiling Gnome ProgramsGiven the extensive number of libraries involved in creating Gnome applications, thecompiler command with all its listed libraries and flags can be very complex to construct.For this reason, Gnome provides the gnome-config script. You place a call to this scriptas an argument to the compiler operation instead of manually listing Gnome librariesand flags. gnome-config takes two options, -cflags and -libs . The -cflags option

G n o m e a n d K D E P r o g r a m m i n g 13

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:47 PM

Color profile: Generic CMYK printer profileComposite Default screen

14 R e d H a t L i n u x : T h e C o m p l e t e R e f e r e n c e

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

will generate all the flags you need, and the -libs option generates the list of necessaryGnome libraries. You do need to specify the libraries you want to use, such as gnomeuiand gnome, as shown here:

gnome-config --cflags –-libs gnome gnomeui

For the compiler operation, you would place the gnome-config operation in backquotes to execute it:

gcc myprog.c –o myprog 'gnome-config -–cflags -–libs gnome gnomeui'

To simplify matters, you can place this operation in a Makefile. In a Makefile, thecompiling is performed separately from the linking. For compiling, you would use agnome-config script with the -cflags option, and for linking you would use the-libs option. In the following example, the CFLAGS and LDFLAGS macros are usedto hold the compiling and linking results, respectively. Notice the use of back quotes inthe code.

makefile

CFLAGS='gnome-config --cflags gnome gnomeui'

LDFLAGS='gnome-config --libs gnome gnomeui'

all: bookrec

bookrec: file.o calc.o

cc $(LDFLAGS) main.o –o bookrec

main.o: main.c

cc $(CFLAGS) main.c

file.o: file.c file.h

cc $(CFLAGS) file.c

Gnome Program ExampleThe hello1.c program is a simple Gnome application in the “Hello World” tradition.The program creates a simple window with a button that displays a message on thestandard output of your terminal window. When the user clicks the Close box(delete_event), the window closes (see Figure 1).

Gnome functions begin with the term “gnome”, whereas GTK functions begin with“gtk”. Notice that the initialization function is a Gnome function, gnome_init. Asexplained earlier, Gnome programs are event-driven: you first define your objects,such as windows, then set their attributes, and then bind signals from events such asmouse clicks to objects like windows and to functions that process these events. Suchfunctions are often referred to as callback functions.

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:47 PM

Color profile: Generic CMYK printer profileComposite Default screen

G n o m e a n d K D E P r o g r a m m i n g 15

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

To compile this program, you can use the following compile command in a Gnometerminal window. Then, just enter hello1 to run it. The -o option specifies the nameof the program, in this case hello. Be sure to use back quotes for the gnome-configsegment.

gcc hello1.c –o hello1 'gnome-config -–cflags -–libs gnome gnomeui'

You would use the following steps to create the hello program listed in hello.c.

■ Define two callback functions: hellomessage and closeprog. hellomessage justoutputs a simple text, “Hello World”. closeprog invokes the gtk_main_quitfunction to end the program.

■ In the main function, define two GtkWidget pointers: app and mybutton. appshould be a pointer to the main application window and mybutton to a simplebutton object.

■ Create a gnome_init function to initialize the Gnome interface.

■ Create a button object using the gtk_button_new_with_label function, andassign its address to the mybutton pointer, as shown in the following codeline. The button will be displayed with the label “Click Me”.

mybutton = gtk_button_new_with_label("Click Me");

■ Create an application window widget using the gnome_app_new function, andassign its address to the app pointer.

■ Use gnome_app_set_contents to place the button in the application window.

■ Use gtk_signal_connect to connect the application with a delete_event signal,which occurs when the user clicks the Close box. Set this to execute the closeprogfunction, which should use gtk_main_quit to end the program.

Figure 1. Gnome window

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:48 PM

Color profile: Generic CMYK printer profileComposite Default screen

■ Use gtk_signal_connect to connect the button to the mouse click event(clicked), and set this to execute the hello function. Whenever the user clicksthe button, “Hello World” should be displayed on the standard output.

■ Use the gtk_widget_show_all function to display the application window andthe button it now contains.

■ Use gtk_main starts the interactive interface.

The contents of the hello1.c program are shown here.

hello.c#include <gnome.h>

void hellomessage( GtkWidget *widget, gpointer data ){

g_print ("Hello World\n");}

gint closeprog ( GtkWidget *widget, GdkEvent *event,gpointer data )

{gtk_main_quit();}

int main( int argc, char *argv[] ){GtkWidget *app;GtkWidget *mybutton;

gnome_init ("", "0.1", argc, argv);

mybutton = gtk_button_new_with_label("Click Me");app = gnome_app_new ("Hello-World", "Hello App");gnome_app_set_contents (GNOME_APP (app), mybutton);gtk_signal_connect (GTK_OBJECT (app), "delete_event",GTK_SIGNAL_FUNC (closeprog),NULL);

gtk_signal_connect (GTK_OBJECT (mybutton), “clicked”,GTK_SIGNAL_FUNC (hellomessage), NULL);gtk_widget_show_all(app);gtk_main ();

return(0);}

GladeInstead of coding complex and detailed statements for all your Gnome widgets, youcan use Glade to automatically generate them. Glade provides a graphical userinterface for creating Gnome widgets, combining them into a GUI interface for your

16 R e d H a t L i n u x : T h e C o m p l e t e R e f e r e n c e

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:48 PM

Color profile: Generic CMYK printer profileComposite Default screen

application. With Glade, creating a Gnome interface is as simple as selecting andplacing widgets onto windows. For each widget you can specify certain propertiessuch as their size, color, and the signals they use. See glade.gnome.org for moreinformation.

You can start Glade by selecting its entry in the Gnome Applications menu. Be surethat you have already installed the Gnome development packages, including Glade.Glade will initially display three separate windows, the main Glade window, thePalette window, and the Property Editor (labeled Properties). To create your interfaceyou will be selecting Gnome widgets in the Palette window. When you select a windowon the Palette, a window will automatically be generated on your desktop. You canthen select different widgets such as buttons and menus, and place them on the window,building your interface. When you create a window, it is listed in the main Gladewindow. As you create more windows for your application interface, icons for themwill be displayed in the Glade window. Figure 2 shows Glade with a window labeledwindow1 that has been created. From the Glade window you can also open previousprojects. When you are finished with Glade, select Exit from the File menu.

To start using Glade, you first create a project. Select New Project from the Filemenu or click on the New icon in the toolbar. The Project Options window will let youselect the directory you want your project placed in (see Figure 3). By default, projectsare placed in a directory called Projects in your home directory. Each project has its

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

G n o m e a n d K D E P r o g r a m m i n g 17

Figure 2. Glade

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:49 PM

Color profile: Generic CMYK printer profileComposite Default screen

18 R e d H a t L i n u x : T h e C o m p l e t e R e f e r e n c e

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

own subdirectory which is given the default name numbered project1. You can specifyyour own name for the Project directory, as well as the subdirectories for the particularprojects. A Glade project file will have the extension .glade, in this directory. The sourcecode files for the project are placed in a subdirectory named src. Glade will generate amain.c and an interface.c source code file for the widgets you create.

The Palette is the key component in Glade. Here you will find all the widgets youcan use to create your interface. The Palette displays three different kinds of widgets:GTK+ Basic, GTK+ Additional, and Gnome. There are buttons for each at the top of thePalette window, and clicking on one will display the widgets available for each category.GTK+ Basic, as the name suggests, provides basic components such as buttons, windows,and menus. GTK+ Additional provides more sophisticated, though less commonlyused, GTK+ widgets. Gnome lists the Gnome widgets. These conform to the functionsin the Gnome libraries. Of particular note is the Gnome application window, whichprovides a Gnome application window with basic menus and toolbar already installed.Figure 4 shows the Glade Palette displaying the GTK+ widgets.

When creating a Gnome interface, bear in mind that Gnome uses a containermethod for holding and placing its widgets. Each widget that you place in a window,cannot be placed directly in the window, but, instead, must be placed in a container. Infact, the key to designing a Gnome interface is to first select the appropriate containerfor the widget you want to add. For example, to place a button in a Gnome window,

Figure 3. Glade project options

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:49 PM

Color profile: Generic CMYK printer profileComposite Default screen

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

you first place a container for it on the window and then place the button in thatcontainer. Containers come in a variety of combinations. You can have several containersstacked on top of each other in rows (horizontal boxes), or set beside each other incolumns (vertical boxes). Containers can be organized into tables, or you can create acontainer that supports fixed positions. When you place a container on a window, youwill be asked to select the number of containers you want. For example, for containersthat fill up rows the length of a window, you would select horizontal box container.When you place it on the window, you will be prompted to enter the number of rowsyou want. The different types of containers are displayed at the bottom of the GTK+Basic Palette window, showing the outline formats for each. Figure 5 shows thecontainers currently available.

Keep in mind that, unless you select the Fixed Position container, the widget willautomatically expand to fill the frame. For example, a button placed in a row containerwill fill the entire length of the window. This is helpful if you are selecting the kind ofwidget that would fit well into that container. For example, if you want to add a menuand a toolbar to a blank window, you would first select and place the horizontal boxescontainer and select three rows. Initially, all three rows will be evenly sized. Then youwould click on the menu widget and place it on the first row. The row will contract tothe size of the menu, and the same for the toolbar placed on the second row.

G n o m e a n d K D E P r o g r a m m i n g 19

Figure 4. Glade Palette

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:49 PM

Color profile: Generic CMYK printer profileComposite Default screen

You can create complex combinations by placing one container inside another. Forexample, you could first create two row containers, placing a menu in one. Then placetwo column containers in the remaining row container. In one column, you could placeseveral button-row containers and put buttons in them. The other column could hold aframe for displaying data.

You can delete any widget, including containers, by right-clicking on them and selectingDelete from the pop-up menu.

To just place a widget anywhere on a surface, you would use the Fixed Positionscontainer. With this container, you can then select any number of widgets, placing eachat different places on the container space. You can move or resize the widget by selectingit to display the anchor points on its corners. Use these to resize it, or use click and dragon the selected widget to move it. In Figure 6, a Fixed Position container has beencreated and two widgets placed on it—a label with the text “Hello World” and a buttonwith the text “Click Me”.

The properties window will display the different properties for a selected widget.The properties window displays several tabbed panels. Here, you can enter the featureslike the text displayed by a widget—should it display text. Widgets like buttons andmenu items will also have a Signals panel where you can select the signals that thatwidget will respond to. You can have several signals for any given widget. To create asignal, you first select the kind of signal from the pop-up menu, then select the handlerfunction that signal will execute. You can also enter data and objects to be passed.Figure 7 shows the Signal panel for the Click Me button.

Once you have created your interface, you then need to generate the source codefor it. Click the Build icon or select Build from the File menu. This will generate main.cand interface.c files. The interface.c file in your project’s src directory will hold the C

20 R e d H a t L i n u x : T h e C o m p l e t e R e f e r e n c e

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

Figure 5. Glade GTK+ containers

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:49 PM

Color profile: Generic CMYK printer profileComposite Default screen

code for your widgets. You can then continue to code your program, adding classesand functions your project develops. You can open your Glade project at any time toadd new windows, dialogs, and their widgets. In the project directory, you can useAutoconf and configure commands to generate an appropriate Make file, and thenuse the make command to create your application.

Gnome App and Menu WidgetsThe GnomeApp widget is the basic widget for Gnome applications. This widget is themain window holding menus, toolbars, and data. You use the gnome_app_new functionto create a new GnomeApp widget. This function takes as its argument the name ofthe application.

To add elements such as toolbars, menus, and status bars to the widget, you justuse the appropriate function. For example, to add a menu, use gnome_app_set_menus,and to add a status bar use gnome_app_set_statusbar. To add just a single toolbar, usegnome_app_set_toolbar, and to add multiple toolbars use gnome_app_add_toolbar.

With the gnome-app-helper functions, you can generate menus and toolbarsautomatically using GnomeUIInfo structures. For toolbars and menus, you can create

G n o m e a n d K D E P r o g r a m m i n g 21

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

Figure 6. Buttons and labels on Glade

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:50 PM

Color profile: Generic CMYK printer profileComposite Default screen

GnomeUIInfo structures for them with the appropriate values and then usegnome_app_create_menus to create menus and gnome_app_create_toolbar to createtoolbars.

With the GNOMEUIINFO_ITEM macro, you can add an item to a menu.The GNOMEUIINFO_SEPARATOR macro adds a separator line, and theGNOMEUIINFO_END macro specifies the end of menu. In the following example,label is the text of the label, tooltip is the tooltip that will be displayed when thepointer moves over that item, and callback is the function that is executed when theuser clicks that item. You can add another argument for an icon image if you want anicon displayed in the menu item. This is usually a .xpm image.

GNOMEUIINFO_ITEM(label, tooltip, callback)

To specify an accelerator key for a particular item, you just place an underscorebefore the letter in the label for the key you want to use. An accelerator key is analternative key you can use to access the menu item. This is usually an ALT key. In

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

22 R e d H a t L i n u x : T h e C o m p l e t e R e f e r e n c e

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

Figure 7. Glade Signal Properties panel

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:50 PM

Color profile: Generic CMYK printer profileComposite Default screen

the following example, the menu item will have an Exit label with the “x” underlined,indicating that you can use an ALT-X key combination to access this item.

GNOMEUIINFO_ITEM("E_xit", "Exit the program", exitfunc)

The GNOMEUINFO_ITEM macro generates the values to be used in a GnomeUIInfostructure. You can assign these values to such a structure. In the following example, amenu is created consisting of an array of GnomeUIInfo structures, and GnomeUIInfomacros are used to assign values to each GnomeUIInfo structure in this array. In thisexample, a simple File menu is created with two entries, one for Open and one for Exit. Aline separator will be displayed between them.

GnomeUIInfo file_menu[] = {

GNOMEUIINFO_ITEM("_Open", "Open a document", openfunc),

GNOMEUIINFO_SEPARATOR,

GNOMEUIINFO_ITEM(“E_xit", "Exit the program", exitfunc),

GNOMEUIINFO_END

};

A number of macros are provided for standard menu items, like the Save and Openentries in a File menu. These take as their arguments the function to be executed whenthe item is selected (cb) and any icon image you want displayed for the entry (data).Here is the syntax for these macros:

GNOMEUIINFO_MENU_OPEN_ITEM(cb, data)

The following example creates the same simple File menu as in the previousexample, but it uses specialized macros to create each item. Here, theGNOMEUIINFO_MENU_EXIT_ITEM macro creates the Exit entry for the menu:

GnomeUIInfo file_menu[] = {

GNOMEUIINFO_MENU_OPEN_ITEM(openfunc),

GNOMEUIINFO_SEPARATOR,

GNOMEUIINFO_MENU_EXIT_ITEM(exitfunc),

GNOMEUIINFO_END

};

For submenus and for menus added to your menu bar, you use the GNOMEUIINFO_SUBTREE(label, tree) macro, where tree is the array of GnomeUIInfo structures to be usedfor that submenu.

G n o m e a n d K D E P r o g r a m m i n g 23

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:50 PM

Color profile: Generic CMYK printer profileComposite Default screen

The following example assigns the File menu defined earlier and an Edit menu toa menu bar. Again, these are GnomeUIInfo structures for which the macros generatevalues. Notice the use of underscores in the labels to designate ALT keys for accessingthe menus.

GnomeUIInfo menubar[] = {

GNOMEUIINFO_SUBTREE("_FILE", file_menu),

GNOMEUIINFO_SUBTREE("_EDIT", edit_menu),

GNOMEUIINFO_END

};

For particular menus on a menu bar, you use the menu tree macros. The treeargument is the array of GnomeUIInfo structures for the menu. For example, the Filemenu can be added to the menu bar with the following statement, where tree is thearray of GnomeUIInfo structures for the File menu:

GNOMEUIINFO_MENU_FILE_TREE (tree)

The following example is a rewritten version of the menu bar assignment usingspecialized macros for the File and Edit menus:

GnomeUIInfo menubar[] = {

GNOMEUIINFO_MENU_FILE_TREE(file_menu, NULL),

GNOMEUIINFO_MENU_EDIT_TREE(edit_menu, NULL),

GNOMEUIINFO_END

};

Once you have defined your menus, you can create them using the gnome_app_create_menus function. This takes as its arguments the Gnome application structureand the pointer to the GnomeUIInfo structures you are using for your menu bar. Inthe previous example, this pointer was the array name “menubar”. Each of theelements making up the menubar array, in turn, references a GnomeUIInfo arrayfor their menu.

gnome_app_create_menus (GNOME_APP (app), menubar);

KDE Development: KDevelopKDE (K Desktop Environment) is organized on a C++ object model with C++ objectscontaining functions with which you can modify the object. Many of the functions areinherited from higher-level KDE classes, while others are defined for a particular type

24 R e d H a t L i n u x : T h e C o m p l e t e R e f e r e n c e

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:51 PM

Color profile: Generic CMYK printer profileComposite Default screen

of object. In a KDE program, you define an object and then use its public and privatefunctions to modify it. For example, you can create a menu object and then use themenu object’s functions to add new menu items to it. KDE uses the Qt Toolkit which isdeveloped by Troll Tech (http://www.troll.no). It is this toolkit that is actually used todisplay and manage GUI objects such as buttons and windows. The Qt Toolkit operatesmuch like the GTK+ Toolkit in Gnome.

Because KDE applications are C++ object-oriented programs, they use a set ofhierarchical object classes contained in the KDE and Qt libraries. Classes lower in thehierarchy will inherit members (functions) from predefined KDE classes higher in thehierarchy, and you can create your own classes and have them inherit members. KDEuses the Qt Toolkit and currently relies on it directly. Unlike Gnome, which can haveits lower-level functions managed by any toolkit, KDE relies solely on the Qt Toolkit.Currently, KDE programming is essentially Qt programming.

KDE and Qt programming rely on an extensive set of classes, each of which usuallyhas a significant number of member functions that manage objects of that class. Thereare far more than can be listed within the size limitations of this book. For a completelisting of the KDE user interface classes, consult the documentation provided on theKDE developer’s site, developer.kde.org. This site includes detailed tutorials, andcomplete reference materials for the KDE API as well as KOM (KDE Object Manager)documentation and Qt reference material. Each class is described in detail, and classtype declarations, including their member function declarations and definitions, aregiven. In addition, consult the KDE and Qt header files. The .h files contain a completelisting of the KDE and Qt classes, along with detailed comments describing theirmember functions.

A widget, like a window or a button, is just an object. You can define a windowobject using a KDE or Qt window class or a button using a KDE or Qt button class.There are several kinds of classes that you can use, depending on the type of windowor button you want. To create a complex widget, such as a window that contains otherwidgets (perhaps toolbars and menus), you would define the subwidgets as childrenof the main widget. When you define a toolbar, you specify a particular window objectas its parent. A subwidget can, in turn, have its own subwidgets, its own children.For example, a menu bar can have a window as its parent and individual menus asits children.

When you declare a C++ object, you usually include arguments in addition to theclass and object name. These arguments are passed to a special function called a constructorthat is executed when the object is defined, which performs any needed setup orinitialization operations for the object.

For widgets, one of these arguments is usually the address of its parent widget.For example, a toolbar will be defined with one of its arguments being the address ofa window object that is its parent. If the widget is a top-level object with no parent,the argument is NULL. With a series of simple object definitions, you can easily createa complex widget.

G n o m e a n d K D E P r o g r a m m i n g 25

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:51 PM

Color profile: Generic CMYK printer profileComposite Default screen

KDE LibrariesA KDE program is simply a C++ program that uses objects whose classes are definedin the KDE and Qt libraries. You use the g++ compiler on your source code files as youwould any other C++ program. g++ is the C++ form of the gcc C compiler. There areseveral KDE libraries, each with an extensive set of classes. Most programs will needat least the kdecore and kdeui libraries. Kdeui holds the KDE user interface classesfor KDE widgets (see Table 7).

26 R e d H a t L i n u x : T h e C o m p l e t e R e f e r e n c e

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

Widget Description

DialogBase A base class that provides basic functionality needed bynearly all dialog boxes

KApplet The KDE Panel Applet class

KButton The class that provides active raise/lower buttons

KButtonBox A container widget for buttons

KCursor A Qt QCursor wrapper allowing “themed” cursors

KDialog A dialog box with extended modeless support

KFontChooser A widget for interactive font selection

KGradientSelector A gradient selector widget

KMenuBar A floatable menu bar

KMessageBox An easy MessageBox dialog box

KNumCheckButton A different type of Check button

KPopupMenu A pop-up menu with a title

KProgress A progress-indicator widget

KSeparator A standard horizontal or vertical separator

KStatusBar A KDE status bar widget

KStatusBarItem An internal class for use in KStatusBar

KStatusBarLabel An internal class for use in KStatusBar

KTMainWindow A KDE top-level main window

KToolBar A floatable toolbar with auto-resize

Table 7. Common KDE Kdeui (User Interface)

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:51 PM

Color profile: Generic CMYK printer profileComposite Default screen

KDevelopKDevelop is an integrated development environment (IDE) for writing K Desktopprograms (see www.kdevelop.org). You can access KDevelop from the Developmententry in the K Desktop menu. When KDevelop first opens, it displays a window withtwo toolbars, a status bar, and three subwindows (see Figure 8). The bottom windowdisplays debugging information. The left window will display the different C++ classesand source code files defined for your project. The right window holds three tabbedpanes: one for header files, one for your source code files, and the other for KDevelopdocumentation.

KDevelop and Qt Designer are not included with the Publisher’s Edition. You will needto download them from the Red Hat FTP site.

To start a project you select New from the Project menu. This will start up theApplication Wizard, shown in Figure 9, that helps you set up the kind of applicationyou want to create. For example, you could create a KDE panel applet, a Konquerorbrowser plug-in, or a standard KDE Desktop interface.

Click the Next button to move to the next window in the Wizard. Here, you specifythe project name and its directory, along with the kind of support files you will need,such as those for documentation (see Figure 10). KDevelop will create a directory withthe same name as the projects. Within that directory, it will create a further subdirectorywith the same name, which will hold the source code files. The source code files for themyhello project would be in the myhello/myhello directory.

On the next screen you can choose to use the CVS versioning system, to help keeptrack of changes and enable many users to work on the same project. Then, templatesfor standard headings in the source and header files are presented. On the Processscreen, you click the Create button to have KDevelop generate any needed files andcreate an initial main.c source code file with required K Desktop functions and classesdefined, as well as links to the needed KDE libraries. Click the Exit key when finished.

G n o m e a n d K D E P r o g r a m m i n g 27

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

Widget Description

KToolBarButton A toolbar button

KToolBarItem A toolbar item

KTopLevelWidget An old KDE top-level window

Table 7. Common KDE Kdeui (User Interface) (continued)

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:51 PM

Color profile: Generic CMYK printer profileComposite Default screen

28 R e d H a t L i n u x : T h e C o m p l e t e R e f e r e n c e

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

For a standard KDE application, KDevelop will automatically generate the codefor an application window, standard menus including File, Edit, and Help menus, astandard icon bar, and a status bar. This initial code will be placed in the source codefile of the same name—for example, myhello.cpp. You can then add in your own classeswith their own function calls and have them invoked from entries you can add to thesemenus or to the toolbar.

You could code your program as you would any C++ program. However, KDevelopalso includes the Qt Designer that you can use to automatically create your KDE windowsand widgets. Once you have created your project, you can then start up Qt Designer byselecting the Dialog Editor entry in the View window. This opens a separate Qt Designer

Figure 8. KDevelop

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:52 PM

Color profile: Generic CMYK printer profileComposite Default screen

G n o m e a n d K D E P r o g r a m m i n g 29

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

window, with toolbars for different KDE widgets (see Figure 11). A subwindow in themain pane is the Property Editor, which will display properties for any selected widgetyou have created.

To create a window, either click on the New icon in the toolbar or select New fromthe File menu. A window will be displayed on the right side of the main pane. To the leftwill be the window’s Property Editor. Here, you can specify the class name of the window,along with its caption, size, and color, and so on. In Figure 11, the class name is hwin andthe caption is My Hello. When you save the window and its widgets, you need to savethem in the directory for the source code used for your project.

Figure 9. KDevelop Application Wizard

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:53 PM

Color profile: Generic CMYK printer profileComposite Default screen

30 R e d H a t L i n u x : T h e C o m p l e t e R e f e r e n c e

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

To add a widget to your window, click on the widget in the toolbar and then clickon where you want to place it on the window, and drag to set the size of the widget.The widget will appear. The Property Editor will automatically display the propertiesfor the selected widget. For example, to create a button, click the Button icon and thenclick in your window. You can then change the text displayed for the button in itsProperty Editor window. Figure 11 shows both a label and a button widget added tothe hwin window. The button has had its display text changed to “Click Me”.

Widgets, like buttons, need to be connected to slots for them to have any effect. Theslot is the function that will activate when a specified event occurs on that button. To setup an event/slot connection for a widget, you need to select the Connect Signal/Slotsentry in the Tools menu, or click on the Connect Signal/Slots button in the secondtoolbar. Then, click on the widget. This opens a window where you can select the eventand the slot to be associated with it.

Figure 10. KDevelop project specifications

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:53 PM

Color profile: Generic CMYK printer profileComposite Default screen

G n o m e a n d K D E P r o g r a m m i n g 31

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

Once you have created your windows and widgets with the Qt Designer, you thenneed to integrate them into your project as source code. When you exit the Qt Designer,KDevelop will set up the necessary instructions in your project’s Makefile to do this.Qt Designer actually saves its code in special .ui files that need to be converted to C++code to be used. KDevelop sets up this process for you. However, you still need tointegrate the KDE windows and widgets into your program. When you created yourmain window in Qt Designer, you gave it a class name. The example in Figure 11 usedhwin as the class name for the main window. You have to integrate this class name intothe overall class structure of your KDE C++ program. You do this by creating a new classin your program that will inherit from the class you created with the Qt Designer. Thisway, all those windows and widget classes that contain the code to create and managethose widgets are inherited automatically into your program and can be referenced bythe new class you created.

To create a new class, select New Class from the Projects menu. This opens awindow where you can specify the name of the new class and any class it inherits

Figure 11. Qt Designer

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:54 PM

Color profile: Generic CMYK printer profileComposite Default screen

from. In Figure 12, a new class called myhellowin is created that will inherit from thehwin class, its base class. Through the myhellowin class, the program can access thewindow and widgets created for the hwin class. Click “Generate a QWidget Childclass”since the classes are derived from the parent QWidget class. Whenever you create anew class, KDevelop generates a corresponding header file for it (.h). If the classinherits from another class, KDevelop will generate an include statement to includethe header file for that class. For example, when the myhellowin class is created, amyhellowin.h file is generated for it. Since that class inherits from the hwin class, aninclude statement in inserted in myhellowin.h that will include the hwin.h file. Thehwin.h file is automatically generated by KDevelop using the hwin.ui file created withQt Designer.

Once you have coded your program, you can build and run it by clicking on theRun icon. You can also elect to just build the project or compile selected files.

32 R e d H a t L i n u x : T h e C o m p l e t e R e f e r e n c e

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

Figure 12. New class in KDevelop

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:55 PM

Color profile: Generic CMYK printer profileComposite Default screen

You can manually generate source code from a Qt Designer .ui file using the uic tool.Redirect the output to the appropriate .h file—for example, uic myhello.ui >myhello.h .

KDE Applications and WidgetsTo create a KDE application, you simply create an object of type KApplication in yourprogram before you define any other KDE objects. The class declaration for KApplicationis contained in the kapp.h file. The definition of a KApplication object takes as itsarguments argc and argv. These operate as they do in C programs, referencing anycommand line arguments the user enters. The following example defines an applicationobject called myapp:

KApplication myapp( argc, argv );

Declarations for different kinds of KDE and Qt objects are located in separate headerfiles. Whenever you define an object of a particular type, be sure to include the headerfile that has its class declaration. For example, to create a main application window, youuse KTMainWindow class, and you need to include the ktmainwindow.h header file.

#include <ktmainwindow.h>

The header files are also extremely helpful as a reference source. They list all themember functions for a particular class and include detailed comments describing eachfunction and its use. The header files will be located in the KDE include directory.Currently for Red Hat, this is the standard include directory, /usr/include. On OpenLinuxand other distributions, it may be the special KDE directory, such as /opt/kde/include.

To define a main window for your application, you use the KTMainWindow class.The following example defines a main window object called mywin:

KTMainWindow mywin;

If you create an application where the main window is the primary interface andyou want the application to close when that window closes, you have to make itthe main widget for the application. To do this, you use the application object’ssetMainWidget function. The main widget could be any widget you want. Be sure topass the address of the widget. You do this by preceding it with the address operator,the ampersand - &. The following example sets the main widget to be the mywinwindow. The address of the mywin widget is passed, &mywin.

myapp.setMainWidget(&mywin);

G n o m e a n d K D E P r o g r a m m i n g 33

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:55 PM

Color profile: Generic CMYK printer profileComposite Default screen

If you are using a pointer to a widget as in the later program examples, you need onlypass the pointer without using the address operator. A pointer already holds the address.

When you define a widget, you will also be defining any of its member functionscontained in its class declaration. See the developer.kde.org documentation for acomplete description of all KDE class declarations, including their member functions.Many of these member functions are designed to let you change the display features ofa widget, such as its color or initial size. For example, to control the display size of theKTMainWindow widget, you use its setGeometry function, as shown here:

mywin.setGeometry(100,100,200,100);

You have to explicitly instruct KDE to show any widget that you want displayed.To do this, you use your widget’s show member function. For example, to have themywin window display, you execute its show function as shown here:

mywin.show();

Once you have defined all your widgets and made any modifications, you can thenrun the application. You do this with the KApplication object’s exec member function.

myapp.exec();

When the user closes the application, control returns to the main function, whichcan then terminate the program. Usually, the return statement with the exec functionwill return any errors that exec may return.

return myapp.exec();

The following program creates a simple KDE application that displays a window:

#include <kapp.h>

#include <ktmainwindow.h>

int main( int argc, char **argv )

{

KApplication myapp( argc, argv );

KTMainWindow mywin;

mywin.setGeometry(100,100,200,100);

34 R e d H a t L i n u x : T h e C o m p l e t e R e f e r e n c e

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:55 PM

Color profile: Generic CMYK printer profileComposite Default screen

myapp.setMainWidget(&mywin);

mywin.show();

return myapp.exec();

}

Many systems will have set up the KDEDIR and QTDIR shell variables. KDEDIRwould contain the path names for the KDE commands, headers, and libraries, andQTDIR would contain the path names for Qt. To specify the header files for KDE youwould use the following:

-I/$KDEDIR/include

Be sure to include the $ before KDEDIR. If KDEDIR is not already defined, you candefine it yourself, assigning the location (if you know it) of the KDE components:

KDEDIR = /opt/kde

On Red Hat Linux, the Qt libraries for 6.0 are placed in the /usr/lib/qt directory,and KDE libraries are mixed with other libraries in the /usr/lib directory. You will nothave to specify a KDE library, and for Qt, you specify the /usr/lib/qt directory. CalderaOpenLinux, along with other distributions, currently defines the KDE libraries to be inthe /opt/kde directory.

In the following example, both the KDE and Qt libraries are specified for themyapp.cpp KDE program.

g++ -I$KDEDIR/include -L$KDEDIR/lib -I$QTDIR/include -L$QTDIR/libs

–lkdecore –lkdeui –lqt myapp.cpp

Signals and SlotsKDE and Qt use signals and slots to allow one widget to communicate with another.Signals and slots are member functions defined in a class that have special capabilities.Signals are emitted by an object when it is activated by some event occurring on it. Forexample, when a user clicks a button object, the button will emit a clicked signal. Thissignal can then be picked up by any other object set up to receive it. Such an object willhave slots that are designated to receive the signal. A slot is just a member function thatexecutes when the object receives a certain signal.

In effect, slots operate like event handlers, and signals can be thought of as events,but KDE and Qt do not operate like standard event-driven GUIs. Instead, the processof event handling is implemented as messages are sent and received by objects. Insteadof focusing on the processing of an event when it occurs, objects manage their own

G n o m e a n d K D E P r o g r a m m i n g 35

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:55 PM

Color profile: Generic CMYK printer profileComposite Default screen

36 R e d H a t L i n u x : T h e C o m p l e t e R e f e r e n c e

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

event tasks as they occur, whether that be receiving or sending signals. A KDE widgetemits a signal when an event occurs on it or when it changes state for some reason.There are several possible signals, among the more common of which are the activatedand clicked signals. So, when an activated signal occurs on a menu item widget, theprocessing function will execute the corresponding function for that item. For example,given a window with a menu that has an Exit item, when a user clicks on an Exit itemin the File menu, a function to exit the program should be executed. The Exit item emitsa signal that is then received by the main window object, which then executes the slotfunction associated with the Exit item.

The connection between the signal from an emitting object to a slot function in areceiving object is created with the object’s connect function. The connect function setsup a connection between a certain signal in a given object with a specific slot functionin another object. Its first argument is the object, the second is the signal, and the last isthe callback function. To specify the signal, you use the SIGNAL macro on the signalname with its parameters. For the callback command function, you use the SLOT macro.Using connect operations, you can also connect a signal to several slots and connectseveral signals to just one slot. In the following example, the clicked signal on thebuttonhi object is connected to the myhello slot function in the mywin object:

connect(buttonhi, SIGNAL(clicked()), mywin, SLOT(myhello()));

Classes composed of several widgets, such as an application window, will oftenhave connections from signals from the different widgets to the main widget. connectoperations are usually placed with the class declaration of the main widget forconnecting signals from its subwidgets to itself. In this case, the main widget (object)can then be referenced with the C++ this pointer reference, which always referencesthe class being declared, as shown next:

connect(buttonhi, SIGNAL(clicked()), this, SLOT(myhello()));

Any class that includes slots or signals must also include a special reference namedQ_OBJECT. This enables the Meta-Object Compiler preprocessor (described next)to set up any signals and slots declared in the class.

Meta-Object Compiler: MOCThough the code for entering signal and slot functions, as well as that for making theconnections, may appear straightforward to the programmer, it actually requires muchmore complex C++ coding. Signal and slot functions need to be preprocessed by theMeta-Object Compiler (MOC) to generate the C++ code that can implement the signaland slot message-connection process. You then include the output of MOC in yoursource code file for compiling. This means that you should place the class declarationsfor any classes that have signals and slots in separate header files. You can thenpreprocess these header files and include the output in the source code.

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:56 PM

Color profile: Generic CMYK printer profileComposite Default screen

You cannot combine the member function definitions with the class declaration.To compile, the class declaration has to first be preprocessed by MOC before it can becombined correctly with the member function definitions. This necessitates placing theclass declaration in a separate file from the member functions so that the class declarationcan be separately preprocessed by MOC.

To declare a class that contains either signals or slots, you would first declare the classin a header file like myobj.h. You do not place the definitions of any of the memberfunctions in the header file, only the class declaration. Note that the class declaration willinclude declarations of the member functions, structures, and variables. In a separatesource code file, you would place the definition of member functions, like myobj.cpp. Amember function definition is the actual code for the function.

For these definitions to be correctly compiled, you have to include the MOCpreprocessed version of its object declaration, not the actual declaration itself. Togenerate the preprocessed MOC versions, you use the class declaration header file andthe moc command, like this:

moc myobj.h –o myobj.moc

In the particular source code files where you are defining member functions for thisobject, you would include the MOC version of the header file that contains the objectdeclaration, not the header file itself. So you would include myobj.moc in the myobj.cppsource code file, not myobj.h.

However, for any other source code files where you are generating an object of thatclass—say, with a new operation—you just include the header file, not the MOC file.So, for any source code file where you only need the class declaration, you include theheader file, such as myobj.h.

For example, suppose in the main.cpp file a myobj object is generated as a variable,whereas in a myobj.cpp file there are function definitions for member functions for themyobj class. Furthermore, suppose the class definition for myobj is in the myobj.hheader file and the MOC version of myobj.h is in the myobj.moc file. In the main.cppfile, you would include the myobj.h file (not myobj.moc), but in the myobj.cpp fileyou would include the myobj.moc file (not myobj.h).

KDE Program Examplehellowin.cpp is a simple program that displays a button in the main window and thenwill display a message box with “Hello World” when clicked. The Hellowin class willhave two slots declared: myhello (to display the message) and myexit (to exit theprogram). The declaration should also include Q_OBJECT. Q_OBJECT is a specialobject used by KDE to connect to the Qt Toolkit objects. The declaration for Hellowinwill be placed in the hellowin.h header file, and all the member function definitionswill be placed in the hellowin.cpp file. Figure 13 shows both the main window with itsExit and Hello buttons and the hello window displayed by this program.

G n o m e a n d K D E P r o g r a m m i n g 37

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:56 PM

Color profile: Generic CMYK printer profileComposite Default screen

38 R e d H a t L i n u x : T h e C o m p l e t e R e f e r e n c e

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

To compile this program, you first need to preprocess the hellowin.h header filewith MOC. Then, you can compile the hellowin.cpp file. Notice that this file shouldinclude the hellowin.moc file, not the hellowin.h file. The compile operations areshown here:

moc –o hellowin.moc hellowin.h

g++ -lkdecore -lkdeui -lqt -o hellowin hellowin.cpp

Depending on your system, you may also need to specify the location of theKDE libraries and header files. These are usually held in a KDE directory specifiedin the KDEDIR system variable. Use the -L option with the /lib directory to specifythe libraries, and -I options with the /include directory for header files, as in-L$KDEDIR/lib and -I$KDEDIR/include. If KDEDIR is not set, then check with yoursystem administrator or with your Linux distribution manual for the location of theKDE libraries and header files. An example of the compile operation specifying thelocation of the KDE include and lib directories is shown here:

g++ -L$KDEDIR/lib -I$KDEDIR/include -lkdecore –lkdeui \

-lqt -o hellowin hellowin.cpp

The displaying of the “Hello World” message will be handled by a KMsgBox object.This class implements a simple dialog box with messages and several buttons, if youwant them. In addition to a simple message dialog box, KMsgBox also supports dialog

Figure 13. KDE Hellowin program

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:56 PM

Color profile: Generic CMYK printer profileComposite Default screen

G n o m e a n d K D E P r o g r a m m i n g 39

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

boxes with Yes and No buttons and Yes, No, and Cancel buttons. Be sure to includekmsgbox.h.

The basic tasks for creating a Hellowin program are listed here.

■ Create the hellowin.h header file that will hold the class definition of theHellowin class.

■ Include header files that contain class definitions for objects such as windows,buttons, and message boxes. In this case, include the header file for the mainwindow, ktmainwindow.h, for a button, qpushbutton.h, and for a messagebox, kmsgbox.h.

■ Define the Hellowin class, derived from the KTMainWindow class. Give theHellowin class two slots (functions) called myhello and myexit. Define twopointers to buttons: buttonhi and buttonExit.

■ The myhello.cpp file will hold the main program. Begin it by including headerfiles and the hellowin.moc file, which needs to be separately generated by theMOC preprocessor.

■ Define a constructor function for the Hellowin class, Hellowin::Hellowin.When an object of that class is defined, this function is automatically executed.

■ Create a button object using QPushButton, and assign it to the buttonhipointer. Set its size and then display it. It is connected to the myhello slot sothat when it is clicked, the myhello function is executed.

■ Create another button object using QPushButton, and assign it to thebuttonExit pointer. Set its size and then display it. Connect it to the myexitslot so that when it is clicked, the myexit function will be executed.

■ Define a closeEvent function for the Hellowin class. This function shouldsimply end the program by invoking the kapp->quit function.

■ Define the myhello function for the Hellowin class. This should display aseparate message box with the message “Hello World”.

■ Define the myexit function for the Hellowin class, and set it to close theHellowin window.

■ In the main function, define a Kapplication called myapp. Define a Hellowinobject called mywin. Then set the size of the object.

■ Use the setMainWidget function for myapp to make the mywin object the mainapplication window.

■ Use mywin.show to show that window.

■ Use myapp.exec to run the application.

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:56 PM

Color profile: Generic CMYK printer profileComposite Default screen

40 R e d H a t L i n u x : T h e C o m p l e t e R e f e r e n c e

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

The code for the hellowin.h file is shown next.

hellowin.h#include <kapp.h>#include <ktmainwindow.h>#include <qpushbutton.h>#include <kmsgbox.h>

class Hellowin : public KTMainWindow{Q_OBJECTpublic:

Hellowin ();void closeEvent(QCloseEvent *);

public slots:void myhello();void myexit();

private:QPushButton *buttonhi;QPushButton *buttonExit;

};

The following is the code for the hellowin.cpp file.

hellowin.cpp#include <kapp.h>#include <ktmainwindow.h>#include <qpushbutton.h>#include "hellowin.moc"#include <kmsgbox.h>

Hellowin::Hellowin () : KTMainWindow(){buttonhi = new QPushButton("Hello", this);buttonhi->setGeometry(45,30,50,25);buttonhi->show();connect(buttonhi, SIGNAL(clicked()), this, SLOT(myhello()));

buttonExit = new QPushButton("Exit", this);buttonExit->setGeometry(105,30,50,25);buttonExit->show();connect(buttonExit, SIGNAL(clicked()), this, SLOT(myexit()));}

void Hellowin::closeEvent(QCloseEvent *){kapp->quit();}

void Hellowin::myhello(){

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:57 PM

Color profile: Generic CMYK printer profileComposite Default screen

G n o m e a n d K D E P r o g r a m m i n g 41

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

KMsgBox::message(0,"Important","Hello World!");}

void Hellowin::myexit(){close();}

int main( int argc, char **argv ){KApplication myapp ( argc, argv, "Hello World!" );Hellowin mywin;mywin.setGeometry(100,100,200,100);

myapp.setMainWidget( &mywin );mywin.show();return myapp.exec();}

MenusTo create menus, you create a KDE menu bar and then add Qt menus with their menuitems to it. You will need to include the qpopupmenu.h header file for the menus andthe kmenubar.h header file for the menu bar, as shown here:

#include <qpopupmenu.h>

#include <kmenubar.h>

You then define a menu bar object, or a pointer to one, and do the same for yourmenus. The class for a menu bar is KMenuBar, and the class for a menu is QPopupMenu.The following example defines pointers to a menu bar and a menu:

KMenuBar *mymenubar;

QPopupMenu *myfilemenu;

If you defined pointers, you can create the menu and menu bar objects with thenew operation as shown here. KMenuBar takes as its argument its parent. Whendefined in a class like a window, where you want the class itself to be the parent, youuse the this pointer.

mymenubar = new KMenuBar(this);

myfilemenu = new QPopupMenu;

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:57 PM

Color profile: Generic CMYK printer profileComposite Default screen

You can then add the menu to the menu bar with the menu bar’s insertItem memberfunction. The first argument is the label you want displayed on the menu bar for themenu, and the second argument is the address of the menu object. The followingexample adds myfilemenu to mymenubar:

mymenubar->insertItem("File", myfilemenu);

Then, to add items to a particular menu, you use the menu object’s insertItemmember function. Its first argument is the label you want displayed for the item, andthe next two arguments are references to a slot function to be executed when the itememits a signal. (This is the same as the slot arguments in the connect function.) Thesecond argument for insertItem is the address of the object that holds the slot function,and the third argument is the slot function in that object to be executed.

The following example creates an Exit item in the myfilemenu menu and connectsit to the myexit slot function in the current object (denoted by the this pointer):

myfilemenu->insertItem("Exit", this, SLOT(myexit()));

Qt ProgrammingKDE currently relies directly on the Qt Toolkit. Using just Qt objects, you can create aninterface with a look and feel similar to KDE. You can create a Qt application usingjust Qt objects and the Qt libraries. This section provides a basic introduction to Qtprogramming. Both the KDE development site at developer.kde.org and the Qt Website at www.trolltech.com provide very detailed documentation and tutorials on Qtprogramming. It is strongly recommended that you consult these resources for adetailed presentation of Qt programming and API references.

42 R e d H a t L i n u x : T h e C o m p l e t e R e f e r e n c e

Complete Reference / Red Hat Linux: TCR / Petersen / 9178-3 / Web Chapter 43

P:\010Comp\CompRef8\178-3\chWeb3.vpMonday, November 19, 2001 3:43:57 PM

Color profile: Generic CMYK printer profileComposite Default screen