The QApplication class manages the application event queue. More...
#include <qapp.h>
Inherits QObject.
The QApplication class is central to Qt. It receives events from the underlying window system and sends them to the destination widgets. An application object must be created before any widgets can be created!
Only one single QApplication object should be created. In fact Qt
complains if you create more than one, and this is normally done
in the main() function. Once a QApplication object has been
created, qApp
(defined as extern QApplication *qApp
)
refers to this object.
Example (a complete Qt application):
#include <qapp.h> // defines QApplication #include <qpushbt.h> // defines QPushButton int main( int argc, char **argv ) { QApplication app( argc, argv ); // create application object QPushButton hello( "Hello, world!" ); // create a push button app.setMainWidget( &hello ); // define as main widget connect( &hello, SIGNAL(clicked()), // clicking the button &app, SLOT(quit()) ); // quits the application hello.show(); // show button return a.exec(); // run main event loop }
Important
Notice that the QApplication object must be created before any widget can
be defined!
Note also that for X11, setMainWidget() may change the main widget according to the -geometry option. To preserve this functionality, you must set your defaults before setMainWidget() and any overrides after.
Examples: xshape/xshape.cpp forever/forever.cpp connect/connect.cpp picture/makepic.cpp drawdemo/drawdemo.cpp hello/hello.cpp picture/showpic.cpp menu/menu.cpp qmag/qmag.cpp cursor/cursor.cpp widgets/widgets.cpp
Constructs an application object with the command line arguments argc and argv.
The global qApp
pointer refers to this application object. Only
one application object should be created.
This application object must be constructed before any paint devices (includes widgets, pixmaps, bitmaps etc.)
Notice that argc and argv might be changed. Qt removes
command line arguments that it recognizes. argc and argv are
can be accessed later by qApp->argc()
and qApp->argv().
The
documentation for argv() contains a detailed description of how to
process command line arguments.
Qt debugging options:
-nograb,
tells Qt to never grab the mouse or the keyboard.
-sync
(only under X11), switches to synchronous mode for
debugging.
The X11 version of Qt support a few more command line options:
-display
display, sets the X display (default is $DISPLAY).
-geometry
geometry, sets the client geometry of the
main widget.
-fn
or -font
font, defines the application font.
-bg
or -background
color, sets the default background color
and an application palette (light and dark shades are calculated).
-fg
or -foreground
color, sets the default foreground color.
-name
name, sets the application name.
-title
title, sets the application title (caption).
-style=
style, sets the application GUI style. Possible values
are motif
and windows
[virtual]
Closes all widgets and cleans up all window system resources.
Sets qApp
to 0.
Returns the number of command line arguments.
The documentation for argv() contains a detailed description of how to process command line arguments.
See also: argv() and QApplication::QApplication().
Returns the command line argument vector.
argv()[0]
is the program name, argv()[1]
is the first argument and
argv()[argc()-1]
is the last argument.
A QApplication object is constructed by passing argc and argv from
the main()
function. Some of the arguments may be recognized as Qt
options removed from the argument vector. For example, the X11
version of Qt knows about -display, -font
and a few more options.
Example:
// showargs.cpp - displays program arguments in a list box #include <qapp.h> #include <qlistbox.h> int main( int argc, char **argv ) { QApplication a( argc, argv ); QListBox b; a.setMainWidget( &b ); for ( int i=0; i<a.argc(); i++ ) // a.argc() == argc b.insertItem( a.argv()[i] ); // a.argv()[i] == argv[i] b.show(); return a.exec(); }
If you run showargs -display unix:0 -font 9x15bold hello world under X11, the list box contains the three strings "showargs", "hello" and "world".
See also: argc() and QApplication::QApplication().
[static]
Sounds the bell, using the default volume and sound.
[static]
Returns a pointer to the application global clipboard.
[static]
Returns TRUE if the application objects are being destroyed.
See also: startingUp().
[static]
Returns the desktop widget (also called the root window).
The desktop widget is useful for obtaining the size of the screen. It can also be used to draw on the desktop.
QWidget *d = QApplication::desktop(); int w=d->width(); // returns screen width int h=d->height(); // returns screen height d->setBackgroundColor( red ); // makes desktop red
Examples: qmag/qmag.cpp
This function enters the main event loop (recursively). Do not call it unless you are an expert.
See also: exit_loop().
Enters the main event loop and waits until quit() is called or the main widget is destroyed. Returns the value that was specified to quit().
It is necessary to call this function to start event handling. The main event loop receives events from the window system and dispatches these to the application widgets.
Generally, no user interaction can take place before calling exec(). As a special case, modal widgets like QMessageBox can be used before calling exec(), because modal widget have a local event loop.
See also: quit() and setMainWidget().
Examples: xshape/xshape.cpp forever/forever.cpp connect/connect.cpp drawdemo/drawdemo.cpp picture/showpic.cpp menu/menu.cpp qmag/qmag.cpp cursor/cursor.cpp widgets/widgets.cpp
[static]
Tells the application to exit with a return code.
After this function has been called, the application leaves the main event loop and returns from the call to exec(). The exec() function returns retcode.
By convention, retcode 0 means success, any non-zero value indicates an error.
Examples: picture/showpic.cpp
This function leaves from a recursive call to the main event loop. Do not call it unless you are an expert.
See also: enter_loop().
[static]
Flushes the X event queue in the X11 implementation. Does nothing on other platforms.
See also: syncX().
Returns the application widget that has the keyboard input focus, or null if no application widget has the focus.
See also: QWidget::setFocus() and QWidget::hasFocus().
[static]
Returns the default application font. There is always an application font, i.e. the returned pointer is guaranteed to be non-null.
See also: setFont(), fontMetrics() and QWidget::font().
[static]
Returns display (screen) font metrics for the application font.
See also: font(), setFont(), QWidget::fontMetrics() and QPainter::fontMetrics().
Returns the main application widget, or 0 if there is not a defined main widget.
See also: setMainWidget().
[virtual]
Sends event to receiver: receiver->event( event )
Returns the value that is returned from the receiver's event handler.
All Qt events are sent using the notify function. Since this function is virtual, you can make a subclass of QApplication and reimplement notify() to get total control of Qt events.
Installing an event filter on qApp
is another way of making an
application-global event hook.
See also: QObject::event() and installEventFilter().
[static]
Returns the active application override cursor.
This function returns 0 if no application cursor has been defined (i.e. the internal cursor stack is empty).
See also: setOverrideCursor() and restoreOverrideCursor().
[static]
Returns a pointer to the default application palette. There is always an application palette, i.e. the returned pointer is guaranteed to be non-null.
See also: setPalette() and QWidget::palette().
[static]
Stores the event in a queue and returns immediatly.
When control returns to the main event loop, all events that are stored in the queue will be sent using the notify() function.
See also: sendEvent().
[slot]
Tells the application to exit with return code 0 (success). Equivalent to calling QApplication::exit( 0 ).
This function is a slot, i.e. you may connect any signal to activate quit().
Example:
QPushButton *quitButton = new QPushButton( "Quit" ); connect( quitButton, SIGNAL(clicked()), qApp, SLOT(quit()) );
See also: exit().
[static]
Restores the effect of setOverrideCursor().
If setOverrideCursor() has been called twice, calling restoreOverrideCursor() will activate the first cursor set. Calling this function a second time restores the original widgets cursors.
Application cursors are stored on an internal stack. setOverrideCursor() pushes the cursor onto the stack, and restoreOverrideCursor() pops the active cursor off the stack. Every setOverrideCursor() must have an corresponding restoreOverrideCursor(), otherwise the stack will get out of sync. overrideCursor() returns 0 if the cursor stack is empty.
See also: setOverrideCursor() and overrideCursor().
Examples: showimg/showimg.cpp
[static]
Sends an event directly to a receiver, using the notify() function. Returns the value that was returned from the event handler.
See also: postEvent() and notify().
[static]
Changes the default application font to font.
The default font depends on the X server in use.
If updateAllWidgets is TRUE, then the font of all existing widgets is set to font.
Widgets created after this call get font as their font.
See also: font(), fontMetrics() and QWidget::setFont().
Sets the main widget of the application.
The special thing about the main widget is that destroying the main widget (i.e. the program calls QWidget::close() or the user double-clicks the window close box) will leave the main event loop and exit the application.
For X11, this function also resizes and moves the main widget according to the -geometry command-line option, so you should set the default geometry before calling setMainWidget().
See also: mainWidget(), exec() and quit().
Examples: xshape/xshape.cpp forever/forever.cpp connect/connect.cpp drawdemo/drawdemo.cpp picture/showpic.cpp menu/menu.cpp qmag/qmag.cpp cursor/cursor.cpp widgets/widgets.cpp
[static]
Sets the application override cursor to cursor.
Application override cursor are intended for showing the user that the application is in a special state, for example during an operation that might take some time.
This cursor will be displayed in all application widgets until restoreOverrideCursor() or another setOverrideCursor() is called.
Application cursors are stored on an internal stack. setOverrideCursor() pushes the cursor onto the stack, and restoreOverrideCursor() pops the active cursor off the stack. Every setOverrideCursor() must have an corresponding restoreOverrideCursor(), otherwise the stack will get out of sync. overrideCursor() returns 0 if the cursor stack is empty.
If replace is TRUE, the new cursor will replace the last override cursor.
Example:
QApplication::setOverrideCursor( waitCursor ); calculateHugeMandelbrot(); // lunch time... QApplication::restoreOverrideCursor();
See also: overrideCursor(), restoreOverrideCursor() and QWidget::setCursor().
Examples: showimg/showimg.cpp
[static]
Changes the default application palette to palette.
If updateAllWidgets is TRUE, then the palette of all existing widgets is set to palette.
Widgets created after this call get palette as their palette.
See also: QWidget::setPalette() and palette().
[static]
Sets the application GUI style to style.
The style parameter can be MacStyle, WindowsStyle, PMStyle
or MotifStyle.
Only MotifStyle
and WindowsStyle
is
supported in the current version of Qt.
See also: style() and QWidget::setStyle().
[static]
Returns TRUE if an application object has not been created yet.
See also: closingDown().
[static]
Returns the GUI style of the application.
See also: setStyle().
[static]
Synchronizes with the X server in the X11 implementation. Does nothing on other platforms.
See also: flushX().
[static]
Returns a pointer to the widget at global screen position (x,y), or a null pointer if there is no Qt widget there.
If child is FALSE and there is a child widget at position (x,y), the top-level widget containing it will be returned.
See also: QCursor::pos(), QWidget::grabMouse() and QWidget::grabKeyboard().
[static]
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it takes.
Returns the Qt version number for the library, typically "1.1".
Obtains information about the system.
The system's word size in bits (typically 32) is returned in *wordSize. The *bigEndian is set to TRUE if this is a big-endian machine, or to FALSE if this is a little-endian machine.
This function calls fatal() with a message if the computer is truely weird (i.e. different endianness for 16 bit and 32 bit integers).
Prints a debug message, or calls the message handler (if it has been installed).
This function takes a format string and a stack arguments, similar to the C printf() function.
Example:
debug( "my window handle = %x", myWidget->id() );
Under X11, the text is printed to stderr. Under Windows, the text is sent to the debugger.
Warning: The internal buffer is limited to 512 bytes (including the 0-terminator.
See also: warning(), fatal(), qInstallMsgHandler() and Debugging
Prints a warning message, or calls the message handler (if it has been installed).
This function takes a format string and a stack arguments, similar to the C printf() function.
Example:
void f( int c ) { if ( c > 200 ) warning( "f: bad argument, c == %d", c ); }
Under X11, the text is printed to stderr. Under Windows, the text is sent to the debugger.
Warning: The internal buffer is limited to 512 bytes (including the 0-terminator.
See also: debug(), fatal(), qInstallMsgHandler() and Debugging
Examples: xshape/xshape.cpp picture/showpic.cpp
Prints a fatal error message and exits, or calls the message handler (if it has been installed).
This function takes a format string and a stack arguments, similar to the C printf() function.
Example:
int divide( int a, int b ) { if ( b == 0 ) // program error fatal( "divide: cannot divide by zero" ); return a/b; }
Under X11, the text is printed to stderr. Under Windows, the text is sent to the debugger.
Warning: The internal buffer is limited to 512 bytes (including the 0-terminator.
See also: debug(), warning(), qInstallMsgHandler() and Debugging
Prints a warning message containing the source code file name and line number if test is FALSE.
This is really a macro defined in qglobal.h.
ASSERT is useful for testing required conditions in your program.
Example:
// // File: div.cpp // #include <qglobal.h> int divide( int a, int b ) { ASSERT( b == 0 ); // this is line 9 return a/b; }
If b
is zero, the ASSERT statement will output the following message
using the warning() function:
ASSERT: "b == 0" in div.cpp (9)
See also: warning() and Debugging
If p is null, a fatal messages says that the program ran out of memory and exits. If p is not null, nothing happens.
This is really a macro defined in qglobal.h.
Warning: CHECK_PTR only works for the development release of the Qt library. In the release library, CHECK_PTR will be substituted with nothing.
Example:
int *a; CHECK_PTR( a = new int[80] ); // never do this! // do this instead: a = new int[80]; CHECK_PTR( a ); // this is fine
See also: fatal() and Debugging
Installs a Qt message handler. Returns a pointer to the message handler previously defined.
The message handler is a function that prints out debug messages, warnings and fatal error messages. The Qt library (debug version) contains hundreds of warning messages that are printed when internal errors (usually invalid function arguments) occur. If you implement your own message handler, you get total control of these messages.
The default message handler prints the message to the standard output under X11 or to the debugger under Windows. If it is a fatal message, the application aborts immediately.
Only one message handler can be defined, since this is usually done on an application-wide basis to control debug output.
To restore the message handler, call qInstallMsgHandler(0).
Example:
#include <qapp.h> #include <stdio.h> #include <stdlib.h> void myMessageOutput( QtMsgType type, const char *msg ) { switch ( type ) { case QtDebugMsg: fprintf( stderr, "Debug: %s\n", msg ); break; case QtWarningMsg: fprintf( stderr, "Warning: %s\n", msg ); break; case QtFatalMsg: fprintf( stderr, "Fatal: %s\n", msg ); abort(); // dump core on purpose } } int main( int argc, char **argv ) { qInstallMsgHandler( myMessageOutput ); QApplication a( argc, argv ); ... return a.exec(); }
See also: debug(), warning(), fatal() and Debugging
Adds a global routine that will be called from the QApplication destructor. This function is normally used to add cleanup routines.
CleanUpFunctions is defined as typedef void
(*CleanUpFunction)();
, i.e. a pointer to a function that
takes no arguments and returns nothing.
Example of use:
static int *global_ptr = 0; void cleanup_ptr() { delete [] global_ptr; } void init_ptr() { global_ptr = new int[100]; // allocate data qAddPostRoutine( cleanup_ptr ); // delete later }
This file is part of the Qt toolkit, copyright © 1995-96 Troll Tech, all rights reserved.
It was generated from the following files: