您的位置:首页 > 移动开发

QT学习笔记(一):QApplication class

2012-02-02 15:06 267 查看
1. QApplication class 控制着整个GUI程序的控制流和主要的配置。QApplication包含了“主消息循环 ”。

    主消息循环会接受所有的窗口系统或者其他来源的消息,并且进行处理和分发;同时,QApplication 也控制

    着该应用程序的初始化,终止化,以及会话管理(session management).  另外,QApplication 也控制着系统

    范围内和应用程序范围内(system-wide and application-wide settings)的设置。

    对于任意一个QT的GUI程序,只有一个QApplication对象,不论它有多少个窗口。对于non-GUI QT application

    我们使用 QCoreApplication 。因为QCoreApplication不会依赖于 QtGui 库。

The QApplication object is accessible through the instance()
function that returns a pointer equivalent to the global qApp pointer.

QApplication's main areas of responsibility are:

It initializes the application with the user's desktop settings such as palette(), font()
anddoubleClickInterval().
It keeps track of these properties in case the user changes the desktop globally, for example through some kind of control panel.

It performs event handling, meaning that it receives events from the underlying window system and dispatches them to the relevant widgets. By using sendEvent()
and postEvent()
you can send your own events to widgets.

It parses common command line arguments and sets its internal state accordingly. See the constructor
documentation below for more details.

It defines the application's look and feel, which is encapsulated in a QStyle object.
This can be changed at runtime with setStyle().

It specifies how the application is to allocate colors. See setColorSpec()
for details.

It provides localization of strings that are visible to the user via translate().

It provides some magical objects like the desktop()
and the clipboard().

It knows about the application's windows. You can ask which widget is at a certain position usingwidgetAt(),
get a list of topLevelWidgets()
and closeAllWindows(),
etc.

It manages the application's mouse cursor handling, see setOverrideCursor()

On the X window system, it provides functions to flush and sync the communication stream, seeflushX()
and syncX().

It provides support for sophisticated session
management. This makes it possible for applications to terminate gracefully when the user logs out, to cancel a shutdown process if termination isn't possible and even to preserve the entire application's state for a future session. See isSessionRestored(),sessionId()
and commitData()
and saveState()
for details.

Since the QApplication object does so much initialization, it must be created before any other
objects related to the user interface are created. QApplication also deals with common command line arguments. Hence, it is usually a good idea to create it before any
interpretation or modification of argv is done in the application
itself.

2.  


QApplication::QApplication ( int & argc, char ** argv )

Initializes the window system and constructs an application object with argc command line arguments
inargv.

Warning: The data referred to by argc and argv must
stay valid for the entire lifetime of the QApplicationobject.
In addition, argc must be greater than zero and argv must
contain at least one valid character string.

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 (including widgets, pixmaps, bitmaps etc.).

Note: argc and argv might
be changed as Qt removes command line arguments that it recognizes.

Qt debugging options (not available if Qt was compiled without the QT_DEBUG flag defined):

-nograb, tells Qt that it must never grab the mouse or the keyboard.

-dograb (only under X11), running under a debugger can cause an implicit -nograb, use -dograb to override.

-sync (only under X11), switches to synchronous mode for debugging.

See Debugging
Techniques for a more detailed explanation.

All Qt programs automatically support the following command line options:

-style= style, sets the application GUI style. Possible values are motif, windows,
and platinum. If you compiled Qt with additional styles or
have additional styles as plugins these will be available to the -style command
line option.

-style style, is the same as listed above.

-stylesheet= stylesheet, sets the application styleSheet.
The value must be a path to a file that contains the Style Sheet. Note: Relative URLs in the
Style Sheet file are relative to the Style Sheet file's path.

-stylesheet stylesheet, is the same as listed above.

-session= session, restores the application from an earlier session.

-session session, is the same as listed above.

-widgetcount, prints debug message at the end about number of widgets left undestroyed and maximum number of widgets existed at the same time

-reverse, sets the application's layout direction to Qt::RightToLeft

-graphicssystem, sets the backend to be used for on-screen widgets and QPixmaps. Available options are raster and opengl.

-qmljsdebugger=, activates the QML/JS debugger with a specified port. The value must be of format port:1234[,block], where block is optional and will make the application wait until a debugger connects to it.

The X11 version of Qt supports some traditional X11 command line options:

-display display, sets the X display (default is $DISPLAY).

-geometry geometry, sets the client geometry of the first window that is shown.

-fn or -font font,
defines the application font. The font should be specified using an X logical font description. Note that this option is ignored when Qt is built with fontconfig support enabled.

-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.

-btn or -button color,
sets the default button color.

-name name, sets the application name.

-title title, sets the application title.

-visual TrueColor, forces the application to use a TrueColor
visual on an 8-bit display.

-ncols count, limits the number of colors allocated in the color cube on an 8-bit display, if
the application is using the QApplication::ManyColor color
specification. If count is 216 then a 6x6x6 color cube is used (i.e. 6 levels of red, 6 of green,
and 6 of blue); for other values, a cube approximately proportional to a 2x3x1 cube is used.

-cmap, causes the application to install a private color map on an 8-bit display.

-im, sets the input method server (equivalent to setting the XMODIFIERS environment variable)

-inputstyle, defines how the input is inserted into the given widget, e.g., onTheSpot makes
the input appear directly in the widget, while overTheSpot makes
the input appear in a box floating over the widget and is not inserted until the editing is done.


X11 Notes

If QApplication fails
to open the X11 display, it will terminate the process. This behavior is consistent with most X11 applications.

See also arguments().

发现一个简单的代码写法: #ifdef Q_WS_X11
bool useGUI = getenv("DISPLAY") != 0;
#else
bool useGUI = true;
#endif
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息