The QCoreApplication class provides an event loop for console Qt applications. 更多...
继承 QObject .
Inherited by QApplication .
The QCoreApplication class provides an event loop for console Qt applications.
This class is used by non-GUI applications to provide their event loop. For non-GUI application that uses Qt, there should be exactly one QCoreApplication object. For GUI applications, see QApplication .
QCoreApplication contains the main event loop, where all events from the operating system (e.g., timer and network events) and other sources are processed and dispatched. It also handles the application's initialization and finalization, as well as system-wide and application-wide settings.
开始事件循环通过调用 exec_ (). Long running operations can call processEvents () to keep the application responsive.
In general, we recommend that you create a QCoreApplication or a QApplication 对象在您的 main() function as early as possible. exec_ () will not return until the event loop exits; e.g., when quit () 被调用。
Several static convenience functions are also provided. The QCoreApplication object is available from instance (). Events can be sent or posted using sendEvent (), postEvent (),和 sendPostedEvents (). Pending events can be removed with removePostedEvents () or flushed with flush ().
类提供 quit () 槽和 aboutToQuit () 信号。
应用程序有 applicationDirPath () 和 applicationFilePath (). Library paths (see QLibrary ) can be retrieved with libraryPaths () 和 manipulated by setLibraryPaths (), addLibraryPath (),和 removeLibraryPath ().
可以添加或移除翻译文件使用 installTranslator () and removeTranslator (). Application strings can be translated using translate ()。 QObject.tr () 和 QObject.trUtf8 () functions are implemented in terms of translate ().
The command line arguments which are passed to QCoreApplication's constructor should be accessed using the arguments () 函数。 Note that some arguments supplied by the user may have been processed and removed by QCoreApplication.
In cases where command line arguments need to be obtained using the argv () function, you must convert them from the local string encoding using QString.fromLocal8Bit ().
On Unix/Linux Qt is configured to use the system locale settings by default. This can cause a conflict when using POSIX functions, for instance, when converting between data types such as floats and strings, since the notation may differ between locales. To get around this problem, call the POSIX function setlocale(LC_NUMERIC,"C") right after initializing QApplication or QCoreApplication to reset the locale that is used for number formatting to "C"-locale.
This enum type defines the 8-bit encoding of character string arguments to translate ():
| 常量 | 值 | 描述 |
|---|---|---|
| QCoreApplication.CodecForTr | 0 | The encoding specified by QTextCodec.codecForTr () (Latin-1 if none has been set). |
| QCoreApplication.UnicodeUTF8 | 1 | UTF-8. |
| QCoreApplication.DefaultCodec | CodecForTr | (Obsolete) Use CodecForTr instead. |
另请参阅 QObject.tr (), QObject.trUtf8 (),和 QString.fromUtf8 ().
Constructs a Qt kernel application. Kernel applications are applications without a graphical user interface. These type of applications are used at the console or as server processes.
argc and argv arguments are processed by the application, and made available in a more convenient form by the arguments () 函数。
警告: The data referred to by argc and argv must stay valid for the entire lifetime of the QCoreApplication object. In addition, argc must be greater than zero and argv must contain at least one valid character string.
前置 path to the beginning of the library path list, ensuring that it is searched for libraries first. If path is empty or already in the path list, the path list is not 改变。
The default path list consists of a single entry, the installation directory for plugins. The default installation directory for plugins is INSTALL/plugins , where INSTALL is the directory where Qt was installed.
In Symbian this function is only useful for adding paths for finding Qt extension plugin stubs, since the OS can only load libraries from the /sys/bin 目录。
另请参阅 removeLibraryPath (), libraryPaths (), and setLibraryPaths ().
Returns the directory that contains the application 可执行文件。
例如,若有安装 Qt 在 C:\Trolltech\Qt 目录,和运行 regexp example, this function will return "C:/Trolltech/Qt/examples/tools/regexp".
On Mac OS X this will point to the directory actually containing the executable, which may be inside of an application bundle (if the application is bundled).
警告: On Linux, this function will try to get the path 从 /proc file system. If that fails, it assumes that argv[0] contains the absolute file name of the executable. The function also assumes that the current directory has not been changed by the application.
In Symbian this function will return the application private directory, not the path to executable itself, as those are always in /sys/bin . If the application is in a read only drive, i.e. ROM, then the private path on the system drive will be returned.
另请参阅 applicationFilePath ().
Returns the file path of the application executable.
例如,若有安装 Qt 在 /usr/local/qt 目录,和运行 regexp example, this function will return "/usr/local/qt/examples/tools/regexp/regexp".
警告: On Linux, this function will try to get the path 从 /proc file system. If that fails, it assumes that argv[0] contains the absolute file name of the executable. The function also assumes that the current directory has not been changed by the application.
另请参阅 applicationDirPath ().
返回应用程序的当前进程 ID。
该函数在 Qt 4.4 引入。
返回命令行自变量列表。
Usually arguments().at(0) is the program name, arguments().at(1) is the first argument, and arguments().last() is the last argument. See the note below about Windows.
Calling this function is slow - you should store the result in a variable when parsing the command line.
警告: On Unix, this list is built from the argc and argv parameters passed to the constructor in the main() function. The string-data in argv is interpreted using QString.fromLocal8Bit (); hence it is not possible to pass, for example, Japanese command line arguments on a system that runs in a Latin1 locale. Most modern Unix systems do not have this limitation, as they are Unicode-based.
On NT-based Windows, this limitation does not apply either. On Windows, the arguments() are not built from the contents of argv/argc, as the content does not support Unicode. Instead, the arguments() are constructed from the return value of GetCommandLine() . As a result of this, the string given by arguments().at(0) might not be the program name on Windows, depending on how the application was started.
For Symbian applications started with RApaLsSession.StartApp one can specify arguments using CApaCommandLine.SetTailEndL function. Such arguments are only available via this method; they will not be passed to main function. Also note that only 8-bit string data set with CApaCommandLine.SetTailEndL is supported by this 函数。
该函数在 Qt 4.1 引入。
另请参阅 applicationFilePath ().
Returns true if the application objects are being destroyed; otherwise returns false.
另请参阅 startingUp ().
重实现自 QObject.event ().
进入主事件循环并等待,直到 exit () is called. Returns the value that was set to exit () (其为 0 若 exit () 被调用凭借 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.
To make your application perform idle processing (i.e. executing a special function whenever there are no pending events), use a QTimer with 0 timeout. More advanced idle processing schemes can be achieved using processEvents ().
推荐把清理代码连接到 aboutToQuit() signal, instead of putting it in your application's main() function because on some platforms the QCoreApplication.exec() call may not return. For example, on Windows when the user logs off, the system terminates the process after Qt closes all top-level windows. Hence, there is no guarantee that the application will have time to exit its event loop and execute code at the end of the main() function after the QCoreApplication.exec() call.
另请参阅 quit (), exit (), processEvents (),和 QApplication.exec ().
告诉应用程序采用返回代码去退出。
After this function has been called, the application leaves the main event loop and returns from the call to exec_ ()。 exec_ () 函数返回 returnCode . If the event loop is not running, this function does nothing.
按约定, returnCode of 0 means success, and any non-zero value indicates an error.
Note that unlike the C library function of the same name, this function does return to the caller -- it is event processing that stops.
Flushes the platform specific event queues.
If you are doing graphical changes inside a loop that does not return to the event loop on asynchronous window systems like X11 or double buffered window systems like Mac OS X, and you want to visualize these changes immediately (e.g. Splash Screens), call this function.
另请参阅 sendPostedEvents ().
This function returns true if there are pending events; otherwise returns false. Pending events can be either from the window system or posted events using postEvent ().
另请参阅 QAbstractEventDispatcher.hasPendingEvents ().
添加翻译文件 translationFile to the list of translation files to be used for translations.
Multiple translation files can be installed. Translations are searched for in the reverse order in which they were installed, so the most recently installed translation file is searched first and the first translation file installed is searched last. The search stops as soon as a translation containing a matching string is found.
Installing or removing a QTranslator , or changing an installed QTranslator generates a LanguageChange event for the QCoreApplication instance. A QApplication instance will propagate the event to all toplevel windows, where a reimplementation of changeEvent can re-translate the user interface by passing user-visible strings via the tr () function to the respective property setters. User-interface classes generated by Qt Designer provide a retranslateUi() function that can be called.
另请参阅 removeTranslator (), translate (), QTranslator.load (),和 Dynamic Translation .
返回指针指向应用程序的 QCoreApplication (或 QApplication ) 实例。
若没有分配实例, null is returned.
Returns a list of paths that the application will search when dynamically loading libraries.
Qt provides default library paths, but they can also be set 使用 qt.conf file. Paths specified in this file will override default values.
This list will include the installation directory for plugins if it exists (the default installation directory for plugins is INSTALL/plugins , where INSTALL is the directory where Qt was installed). The directory of the application executable (NOT the working directory) is always added, as well as the colon separated entries of the QT_PLUGIN_PATH environment 变量。
若希望遍历列表,可以使用 foreach 伪关键字:
foreach (const QString &path, app.libraryPaths()) do_something(path);
另请参阅 setLibraryPaths (), addLibraryPath (), removeLibraryPath (), QLibrary ,和 如何创建 Qt 插件 .
发送 event to receiver : receiver ->event( event ). Returns the value that is returned from the receiver's event handler. Note that this function is called for all events sent to any object in any thread.
For certain types of events (e.g. mouse and key events), the event will be propagated to the receiver's parent and so on up to the top-level object if the receiver is not interested in the event (i.e., it returns false).
There are five different ways that events can be processed; reimplementing this virtual function is just one of them. All five approaches are listed below:
另请参阅 QObject.event () 和 installEventFilter ().
event argument has it's ownership transferred to Qt.
添加事件 event ,采用对象 receiver as the receiver of the event, to an event queue and returns immediately.
The event must be allocated on the heap since the post event queue will take ownership of the event and delete it once it has been posted. It is not safe to access the event after it has been posted.
When control returns to the main event loop, all events that are stored in the queue will be sent using the notify () 函数。
Events are processed in the order posted. For more control over the processing order, use the postEvent() overload below, which takes a priority argument. This function posts all event with a Qt.NormalEventPriority .
注意: 此函数是 thread-safe .
另请参阅 sendEvent (), notify (),和 sendPostedEvents ().
event argument has it's ownership transferred to Qt.
此函数重载 postEvent ().
添加事件 event ,采用对象 receiver as the receiver of the event, to an event queue and returns immediately.
The event must be allocated on the heap since the post event queue will take ownership of the event and delete it once it has been posted. It is not safe to access the event after it has been posted.
When control returns to the main event loop, all events that are stored in the queue will be sent using the notify () 函数。
Events are sorted in descending priority order, i.e. events with a high priority are queued before events with a lower priority 。 priority can be any integer value, i.e. between INT_MAX and INT_MIN, inclusive; see Qt.EventPriority for more details. Events with equal priority will be processed in the order posted.
注意: 此函数是 thread-safe .
该函数在 Qt 4.3 引入。
另请参阅 sendEvent (), notify (), sendPostedEvents (), and Qt.EventPriority .
Processes all pending events for the calling thread according to the specified flags until there are no more events to process.
You can call this function occasionally when your program is busy performing a long operation (e.g. copying a file).
In event you are running a local loop which calls this function continuously, without an event loop, the DeferredDelete events will not be processed. This can affect the behaviour of widgets, e.g. QToolTip 依赖 DeferredDelete events to function properly. An alternative would be to call sendPostedEvents() 从本地循环中。
Calling this function processes events only for the calling thread.
注意: 此函数是 thread-safe .
另请参阅 exec_ (), QTimer , QEventLoop.processEvents (), flush (),和 sendPostedEvents ().
此函数重载 processEvents ().
处理调用线程待决事件以 maxtime milliseconds or until there are no more events to process, whichever is shorter.
You can call this function occasionally when you program is busy doing a long operation (e.g. copying a file).
Calling this function processes events only for the calling thread.
注意: 此函数是 thread-safe .
另请参阅 exec_ (), QTimer ,和 QEventLoop.processEvents ().
This method is also a Qt slot with the C++ signature void quit() .
Tells the application to exit with return code 0 (success). Equivalent to calling QCoreApplication.exit(0).
常见连接当最后可见首要窗口 (即:窗口没有父级) 被关闭时。 QApplication.lastWindowClosed () signal to quit(), and you also often connect e.g. QAbstractButton.clicked () 或 signals in QAction , QMenu ,或 QMenuBar to it.
范例:
QPushButton *quitButton = new QPushButton("Quit"); connect(quitButton, SIGNAL(clicked()), &app, SLOT(quit()));
另请参阅 exit (), aboutToQuit (),和 QApplication.lastWindowClosed ().
移除 path 从库路径列表。若 path 为空或不在路径列表中,列表无变化。
另请参阅 addLibraryPath (), libraryPaths (), and setLibraryPaths ().
Removes all events posted using postEvent () for receiver .
The events are not dispatched, instead they are removed from the queue. You should never need to call this function. If you do call it, be aware that killing events may cause receiver to break one or more invariants.
注意: 此函数是 thread-safe .
此函数重载 removePostedEvents ().
Removes all events of the given eventType that were posted using postEvent () for receiver .
The events are not dispatched, instead they are removed from the queue. You should never need to call this function. If you do call it, be aware that killing events may cause receiver to break one or more invariants.
若 receiver is null, the events of eventType are removed for all objects. If eventType is 0, all the events are removed for receiver .
注意: 此函数是 thread-safe .
该函数在 Qt 4.3 引入。
移除翻译文件 translationFile 从 list of translation files used by this application. (It does not delete the translation file from the file system.)
另请参阅 installTranslator (), translate (),和 QObject.tr ().
发送事件 event 直接到接收者 receiver , 使用 notify () function. Returns the value that was returned from the event handler.
事件 not deleted when the event has been sent. The normal approach is to create the event on the stack, for example:
QMouseEvent event(QEvent.MouseButtonPress, pos, 0, 0, 0); QApplication.sendEvent(mainWindow, &event);
另请参阅 postEvent () 和 notify ().
Immediately dispatches all events which have been previously queued with QCoreApplication.postEvent () and which are for the object receiver and have the event type event_type .
来自窗口系统的事件 not dispatched by this function, but by processEvents ().
若 receiver is null, the events of event_type are sent for all objects. If event_type is 0, all the events are sent for receiver .
注意: This method must be called from the same thread as its QObject 参数, receiver .
此函数重载 sendPostedEvents ().
Dispatches all posted events, i.e. empties the event queue.
设置属性 attribute if on is true; otherwise clears the attribute.
One of the attributes that can be set with this method is Qt.AA_ImmediateWidgetCreation . It tells Qt to create toplevel windows immediately. Normally, resources for widgets are allocated on demand to improve efficiency and minimize resource usage. Therefore, if it is important to minimize resource consumption, do not set this attribute.
另请参阅 testAttribute ().
把要搜索的目录列表 (当加载库时) 设为 paths . All existing paths will be deleted and the path list will consist of the paths given in paths .
In Symbian this function is only useful for setting paths for finding Qt extension plugin stubs, since the OS can only load libraries from the /sys/bin 目录。
另请参阅 libraryPaths (), addLibraryPath (), removeLibraryPath (), and QLibrary .
Returns true if an application object has not been created yet; otherwise returns false.
另请参阅 closingDown ().
Returns true if attribute attribute is set; otherwise returns false.
另请参阅 setAttribute ().
Returns the translation text for sourceText , by querying the installed translation files. The translation files are searched from the most recently installed file back to the first installed 文件。
QObject.tr () 和 QObject.trUtf8 () provide this functionality more conveniently.
context is typically a class name (e.g., "MyDialog") and sourceText is either English text or a short identifying text.
disambiguation is an identifying string, for when the same sourceText is used in different roles within the same context. By default, it is null.
见 QTranslator and QObject.tr () documentation for more information about contexts, disambiguations and comments.
encoding indicates the 8-bit encoding of character strings.
n is used in conjunction with %n to support plural forms. See QObject.tr () for details.
If none of the translation files contain a translation for sourceText in context , this function returns a QString equivalent of sourceText . The encoding of sourceText is specified by encoding ; it defaults to CodecForTr .
This function is not virtual. You can use alternative translation techniques by subclassing QTranslator .
警告: This method is reentrant only if all translators are installed before calling this method. Installing or removing translators while performing translations is not supported. Doing so will most likely result in crashes or other undesirable behavior.
注意: 此函数是 可重入 .
该函数在 Qt 4.5 引入。
另请参阅 QObject.tr (), installTranslator (), and QTextCodec.codecForTr ().
此函数重载 translate ().
The message procedure calls this function for every message received. Reimplement this function if you want to process window messages msg that are not processed by Qt. If you don't want the event to be processed by Qt, then return true and set result to the value that the window procedure should return. Otherwise return false.
It is only directly addressed messages that are filtered. To handle system wide messages, such as messages from a registered hot key, you need to install an event filter on the event dispatcher, which is returned from QAbstractEventDispatcher.instance ().
This is the default overload of this signal.
This signal is emitted when the application is about to quit the main event loop, e.g. when the event loop level drops to zero. This may happen either after a call to quit () from inside the application or when the users shuts down the entire desktop session.
The signal is particularly useful if your application has to do some last-second cleanup. Note that no user interaction is possible in this state.
另请参阅 quit ().