QCoreApplication

继承者: QApplication

概要

函数

虚函数

信号

静态函数

详细描述

PySide.QtCore.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 PySide.QtCore.QCoreApplication object. For GUI applications, see PySide.QtGui.QApplication .

PySide.QtCore.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 PySide.QtCore.QCoreApplication.processEvents() to keep the application responsive.

In general, we recommend that you create a PySide.QtCore.QCoreApplication PySide.QtGui.QApplication 对象在您的 main() function as early as possible. exec() will not return until the event loop exits; e.g., when PySide.QtCore.QCoreApplication.quit() 被调用。

Several static convenience functions are also provided. The PySide.QtCore.QCoreApplication object is available from PySide.QtCore.QCoreApplication.instance() . Events can be sent or posted using PySide.QtCore.QCoreApplication.sendEvent() , PySide.QtCore.QCoreApplication.postEvent() ,和 PySide.QtCore.QCoreApplication.sendPostedEvents() . Pending events can be removed with PySide.QtCore.QCoreApplication.removePostedEvents() or flushed with PySide.QtCore.QCoreApplication.flush() .

类提供 PySide.QtCore.QCoreApplication.quit() slot and an PySide.QtCore.QCoreApplication.aboutToQuit() 信号。

访问命令行自变量

The command line arguments which are passed to PySide.QtCore.QCoreApplication ‘s constructor should be accessed using the PySide.QtCore.QCoreApplication.arguments() function. Note that some arguments supplied by the user may have been processed and removed by PySide.QtCore.QCoreApplication .

In cases where command line arguments need to be obtained using the PySide.QtCore.QCoreApplication.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 PySide.QtGui.QApplication or PySide.QtCore.QCoreApplication to reset the locale that is used for number formatting to “C”-locale.

class PySide.QtCore. QCoreApplication ( arg__1 )
参数: arg__1 PySequence
PySide.QtCore.QCoreApplication. 编码

This enum type defines the 8-bit encoding of character string arguments to PySide.QtCore.QCoreApplication.translate() :

常量 描述
QCoreApplication.CodecForTr The encoding specified by QTextCodec.codecForTr() (Latin-1 if none has been set).
QCoreApplication.UnicodeUTF8 UTF-8.
QCoreApplication.DefaultCodec (Obsolete) Use CodecForTr 代替。

另请参阅

QObject.tr() QObject.trUtf8() QString.fromUtf8()

PySide.QtCore.QCoreApplication. aboutToQuit ( )
static PySide.QtCore.QCoreApplication. addLibraryPath ( arg__1 )
参数: arg__1 – unicode

前置 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 changed.

The default path list consists of a single entry, the installation directory for plugins. The default installation directory for plugins is INSTALL/plugins ,其中 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 目录。

static PySide.QtCore.QCoreApplication. applicationDirPath ( )
返回类型: unicode

返回包含应用程序可执行文件的目录。

例如,若有安装 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 from the /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.

static PySide.QtCore.QCoreApplication. applicationFilePath ( )
返回类型: unicode

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 from the /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.

static PySide.QtCore.QCoreApplication. applicationName ( )
返回类型: unicode

This property holds the name of this application.

值用于 PySide.QtCore.QSettings 类当使用空构造函数构造它时。这节省必须重复此信息每次 PySide.QtCore.QSettings 对象被创建。

static PySide.QtCore.QCoreApplication. applicationPid ( )
返回类型: PySide.QtCore.qint64

返回应用程序的当前进程 ID。

static PySide.QtCore.QCoreApplication. applicationVersion ( )
返回类型: unicode

This property holds the version of this application.

static PySide.QtCore.QCoreApplication. arguments ( )
返回类型: 字符串列表

返回命令行自变量列表。

Usually PySide.QtCore.QCoreApplication.arguments() .at(0) is the program name, PySide.QtCore.QCoreApplication.arguments() .at(1) is the first argument, and PySide.QtCore.QCoreApplication.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 PySide.QtCore.QCoreApplication.arguments() are not built from the contents of argv/argc, as the content does not support Unicode. Instead, the PySide.QtCore.QCoreApplication.arguments() are constructed from the return value of GetCommandLine(). As a result of this, the string given by PySide.QtCore.QCoreApplication.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 function.

static PySide.QtCore.QCoreApplication. closingDown ( )
返回类型: PySide.QtCore.bool

Returns true if the application objects are being destroyed; otherwise returns false.

static PySide.QtCore.QCoreApplication. exec_ ( )
返回类型: PySide.QtCore.int

进入主事件循环并等待,直到 PySide.QtCore.QCoreApplication.exit() is called. Returns the value that was set to PySide.QtCore.QCoreApplication.exit() (which is 0 if PySide.QtCore.QCoreApplication.exit() is called via PySide.QtCore.QCoreApplication.quit() ).

有必要调用此函数以启动事件处理。主事件循环从窗口系统接收事件,并将其分派给应用程序 Widget。

要使应用程序履行空闲处理 (即:执行特殊函数每当没有待决事件时),使用 PySide.QtCore.QTimer 采用 0 超时。更高级空闲处理方案可以达成使用 PySide.QtCore.QCoreApplication.processEvents() .

推荐把清理代码连接到 PySide.QtCore.QCoreApplication.aboutToQuit() 信号,而非将其放入应用程序的 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() 调用。

static PySide.QtCore.QCoreApplication. exit ( [ retcode=0 ] )
参数: retcode PySide.QtCore.int

告诉应用程序采用返回代码去退出。

After this function has been called, the application leaves the main event loop and returns from the call to exec() exec() function returns returnCode . If the event loop is not running, this function does nothing.

按约定, returnCode 0 意味着成功,而任何非零值指示出错。

注意:不像同名 C 库函数,此函数 does return to the caller – it is event processing that stops.

static PySide.QtCore.QCoreApplication. flush ( )

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.

static PySide.QtCore.QCoreApplication. hasPendingEvents ( )
返回类型: PySide.QtCore.bool

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 PySide.QtCore.QCoreApplication.postEvent() .

PySide.QtCore.QCoreApplication. init ( )
static PySide.QtCore.QCoreApplication. installTranslator ( messageFile )
参数: messageFile PySide.QtCore.QTranslator

添加翻译文件 translationFile 到用于翻译的翻译文件列表。

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 PySide.QtCore.QTranslator , or changing an installed PySide.QtCore.QTranslator generates a LanguageChange event for the PySide.QtCore.QCoreApplication instance. A PySide.QtGui.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.

static PySide.QtCore.QCoreApplication. instance ( )
返回类型: PySide.QtCore.QCoreApplication

返回指针指向应用程序的 PySide.QtCore.QCoreApplication (或 PySide.QtGui.QApplication ) 实例。

若没有分配实例, null 被返回。

static PySide.QtCore.QCoreApplication. libraryPaths ( )
返回类型: 字符串列表

返回将搜索的应用程序路径列表当动态加载库时。

Qt provides default library paths, but they can also be set using a 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 ,其中 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 variable.

若希望遍历列表,可以使用 foreach 伪关键字:

for path in app.libraryPaths():
    do_something(path)
												
PySide.QtCore.QCoreApplication. notify ( arg__1 , arg__2 )
参数:
返回类型:

PySide.QtCore.bool

发送 event to receiver : receiver ->event( event )。返回来自接收者的事件处理程序返回的值。注意:此函数被调用,对于被发送给在任何线程中的任何对象的所有事件。

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

有 5 种不同办法可以处理事件。重新实现此虚函数只是它们之一。以下列出了所有 5 种途径:

PySide.QtCore.QCoreApplication. notifyInternal ( receiver , event )
参数:
返回类型:

PySide.QtCore.bool

static PySide.QtCore.QCoreApplication. organizationDomain ( )
返回类型: unicode

This property holds the Internet domain of the organization that wrote this application.

值用于 PySide.QtCore.QSettings 类当使用空构造函数构造它时。这节省必须重复此信息每次 PySide.QtCore.QSettings 对象被创建。

在 Mac, PySide.QtCore.QSettings 使用 PySide.QtCore.QCoreApplication.organizationDomain() as the organization if it's not an empty string; otherwise it uses PySide.QtCore.QCoreApplication.organizationName() . On all other platforms, PySide.QtCore.QSettings 使用 PySide.QtCore.QCoreApplication.organizationName() as the organization.

static PySide.QtCore.QCoreApplication. organizationName ( )
返回类型: unicode

This property holds the name of the organization that wrote this application.

值用于 PySide.QtCore.QSettings 类当使用空构造函数构造它时。这节省必须重复此信息每次 PySide.QtCore.QSettings 对象被创建。

在 Mac, PySide.QtCore.QSettings 使用 PySide.QtCore.QCoreApplication.organizationDomain() as the organization if it's not an empty string; otherwise it uses PySide.QtCore.QCoreApplication.organizationName() . On all other platforms, PySide.QtCore.QSettings 使用 PySide.QtCore.QCoreApplication.organizationName() as the organization.

static PySide.QtCore.QCoreApplication. postEvent ( receiver , event )
参数:

添加事件 event ,采用对象 receiver 作为事件接收者,到事件队列并立即返回。

事件必须在堆上被分配,因为 Post 事件队列将获取事件的所有权,且事件一旦已被张贴就会被删除。它是 not safe 去访问事件,其它已被张贴之后。

When control returns to the main event loop, all events that are stored in the queue will be sent using the PySide.QtCore.QCoreApplication.notify() 函数。

Events are processed in the order posted. For more control over the processing order, use the PySide.QtCore.QCoreApplication.postEvent() overload below, which takes a priority argument. This function posts all event with a Qt.NormalEventPriority .

static PySide.QtCore.QCoreApplication. postEvent ( receiver , event , priority )
参数:

此函数重载 PySide.QtCore.QCoreApplication.postEvent() .

添加事件 event ,采用对象 receiver 作为事件接收者,到事件队列并立即返回。

事件必须在堆上被分配,因为 Post 事件队列将获取事件的所有权,且事件一旦已被张贴就会被删除。它是 not safe 去访问事件,其它已被张贴之后。

When control returns to the main event loop, all events that are stored in the queue will be sent using the PySide.QtCore.QCoreApplication.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.

static PySide.QtCore.QCoreApplication. processEvents ( flags , maxtime )
参数:
  • flags PySide.QtCore.QEventLoop.ProcessEventsFlags
  • maxtime PySide.QtCore.int
static PySide.QtCore.QCoreApplication. processEvents ( [ flags=QEventLoop.AllEvents ] )
参数: flags PySide.QtCore.QEventLoop.ProcessEventsFlags
static PySide.QtCore.QCoreApplication. quit ( )

Tells the application to exit with return code 0 (success). Equivalent to calling QCoreApplication::exit(0).

常见连接当最后可见首要窗口 (即:窗口没有父级) 被关闭时。 QApplication.lastWindowClosed() signal to PySide.QtCore.QCoreApplication.quit() , and you also often connect e.g. QAbstractButton.clicked() or signals in PySide.QtGui.QAction , PySide.QtGui.QMenu ,或 PySide.QtGui.QMenuBar 到它。

范例:

quitButton = QPushButton("Quit")
quitButton.clicked.connect(app.quit)
												
static PySide.QtCore.QCoreApplication. removeLibraryPath ( arg__1 )
参数: arg__1 – unicode

移除 path 从库路径列表。若 path 为空或不在路径列表中,列表无变化。

static PySide.QtCore.QCoreApplication. removePostedEvents ( receiver )
参数: receiver PySide.QtCore.QObject

Removes all events posted using PySide.QtCore.QCoreApplication.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.

static PySide.QtCore.QCoreApplication. removePostedEvents ( receiver , eventType )
参数:

此函数重载 PySide.QtCore.QCoreApplication.removePostedEvents() .

Removes all events of the given eventType that were posted using PySide.QtCore.QCoreApplication.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 .

static PySide.QtCore.QCoreApplication. removeTranslator ( messageFile )
参数: messageFile PySide.QtCore.QTranslator

移除翻译文件 translationFile from the list of translation files used by this application. (It does not delete the translation file from the file system.)

static PySide.QtCore.QCoreApplication. sendEvent ( receiver , event )
参数:
返回类型:

PySide.QtCore.bool

发送事件 event 直接到接收者 receiver ,使用 PySide.QtCore.QCoreApplication.notify() function. Returns the value that was returned from the event handler.

事件 not 被删除,当事件被发送后。正常途径是在堆栈上创建事件,例如:

event = QMouseEvent(QEvent.MouseButtonPress, pos, 0, 0, 0)
QApplication.sendEvent(mainWindow, event)
												
static PySide.QtCore.QCoreApplication. sendPostedEvents ( receiver , event_type )
参数:

立即分派之前队列的所有事件采用 QCoreApplication.postEvent() and which are for the object receiver 和事件类型 event_type .

来自窗口系统的事件 not 通过此函数分派,而是通过 PySide.QtCore.QCoreApplication.processEvents() .

receiver is null, the events of event_type 为所有对象发送。若 event_type 为 0,所有事件被发送给 receiver .

注意

This method must be called from the same thread as its PySide.QtCore.QObject 参数, receiver .

static PySide.QtCore.QCoreApplication. sendPostedEvents ( )

此函数重载 PySide.QtCore.QCoreApplication.sendPostedEvents() .

Dispatches all posted events, i.e. empties the event queue.

static PySide.QtCore.QCoreApplication. sendSpontaneousEvent ( receiver , event )
参数:
返回类型:

PySide.QtCore.bool

static PySide.QtCore.QCoreApplication. setApplicationName ( application )
参数: application – unicode

This property holds the name of this application.

值用于 PySide.QtCore.QSettings 类当使用空构造函数构造它时。这节省必须重复此信息每次 PySide.QtCore.QSettings 对象被创建。

static PySide.QtCore.QCoreApplication. setApplicationVersion ( version )
参数: version – unicode

This property holds the version of this application.

static PySide.QtCore.QCoreApplication. setAttribute ( attribute [ , on=true ] )
参数:
static PySide.QtCore.QCoreApplication. setLibraryPaths ( arg__1 )
参数: arg__1 – list of strings

把要搜索的目录列表 (当加载库时) 设为 paths 。所有现有路径被删除且路径列表将由给定路径组成按 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 目录。

static PySide.QtCore.QCoreApplication. setOrganizationDomain ( orgDomain )
参数: orgDomain – unicode

This property holds the Internet domain of the organization that wrote this application.

值用于 PySide.QtCore.QSettings 类当使用空构造函数构造它时。这节省必须重复此信息每次 PySide.QtCore.QSettings 对象被创建。

在 Mac, PySide.QtCore.QSettings 使用 PySide.QtCore.QCoreApplication.organizationDomain() as the organization if it's not an empty string; otherwise it uses PySide.QtCore.QCoreApplication.organizationName() . On all other platforms, PySide.QtCore.QSettings 使用 PySide.QtCore.QCoreApplication.organizationName() as the organization.

static PySide.QtCore.QCoreApplication. setOrganizationName ( orgName )
参数: orgName – unicode

This property holds the name of the organization that wrote this application.

值用于 PySide.QtCore.QSettings 类当使用空构造函数构造它时。这节省必须重复此信息每次 PySide.QtCore.QSettings 对象被创建。

在 Mac, PySide.QtCore.QSettings 使用 PySide.QtCore.QCoreApplication.organizationDomain() as the organization if it's not an empty string; otherwise it uses PySide.QtCore.QCoreApplication.organizationName() . On all other platforms, PySide.QtCore.QSettings 使用 PySide.QtCore.QCoreApplication.organizationName() as the organization.

static PySide.QtCore.QCoreApplication. startingUp ( )
返回类型: PySide.QtCore.bool

Returns true if an application object has not been created yet; otherwise returns false.

static PySide.QtCore.QCoreApplication. testAttribute ( attribute )
参数: attribute PySide.QtCore.Qt.ApplicationAttribute
返回类型: PySide.QtCore.bool
static PySide.QtCore.QCoreApplication. translate ( context , key , disambiguation , encoding , n )
参数:
返回类型:

unicode

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

QObject.tr() and 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.

PySide.QtCore.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() 了解细节。

If none of the translation files contain a translation for sourceText in context , this function returns a PySide.QtCore.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 PySide.QtCore.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.

static PySide.QtCore.QCoreApplication. translate ( context , key [ , disambiguation=0 [ , encoding=CodecForTr ] ] )
参数:
返回类型:

unicode

此函数重载 PySide.QtCore.QCoreApplication.translate() .

PySide.QtCore.QCoreApplication. unixSignal ( arg__1 )
参数: arg__1 PySide.QtCore.int
class QCoreApplication ( args )

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.

args argument is processed by the application, and made available in a more convenient form by the arguments() 方法。