ThinkSSL🔒 一键申购 5分钟快速签发 30天无理由退款 购买更放心 广告
[TOC] ## 概述 常用函数 ``` void addLibraryPath(const QString &path) QString applicationDirPath() QString applicationFilePath() QString applicationName() qint64 applicationPid() QString applicationVersion() QStringList arguments() bool closingDown() int exec() void exit(int returnCode = 0) QCoreApplication * instance() QStringList libraryPaths() QString organizationDomain() QString organizationName() void postEvent(QObject *receiver, QEvent *event, int priority = Qt::NormalEventPriority) void processEvents(QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents) void processEvents(QEventLoop::ProcessEventsFlags flags, int maxtime) void removeLibraryPath(const QString &path) void removePostedEvents(QObject *receiver, int eventType = 0) bool sendEvent(QObject *receiver, QEvent *event) void sendPostedEvents(QObject *receiver = Q_NULLPTR, int event_type = 0) void setApplicationName(const QString &application) void setApplicationVersion(const QString &version) void setAttribute(Qt::ApplicationAttribute attribute, bool on = true) void setLibraryPaths(const QStringList &paths) void setOrganizationDomain(const QString &orgDomain) void setOrganizationName(const QString &orgName) ``` ### processEvents **应用场景一:处理密集耗时的事情** - 有时候需要处理一些跟界面无关的但非常耗时的事情,这些事情跟界面在同一个线程中,由于时间太长,导致界面无法响应,处于“假死”状态。 2. 例如:在应用程序中保存文件到硬盘上,从开始保存直到文件保存完毕,程序不响应用户的任何操作,窗口也不会重新绘制,从而处于“无法响应”状态 ``` connect(button, &QPushButton::clicked, [=]() { timer->start(1); for (int i = 0; i < 2000000000; i++){ QCoreApplication::processEvents(); }; timer->stop(); }); ``` ### postEvent - 异步发送事件 ``` void QCoreApplication::postEvent(QObject *receiver, QEvent *event, int priority = Qt::NormalEventPriority) ``` 利用 Application::postEvent() 分发事件,或使用队列式的信号槽连接, 都是用于线程间通信的安全机制--但需要接收线程处于事件循环中 ## 示例 有时候需要处理一些跟界面无关的但非常耗时的事情,这些事情跟界面在同一个线程中,由于时间太长,导致界面无法响应,处于“假死”状态。例如:在应用程序中保存文件到硬盘上,从开始保存直到文件保存完毕,程序不响应用户的任何操作,窗口也不会重新绘制,从而处于“无法响应”状态