Qt5 QApplication类---基本用法

                                                       留得青山在, 不怕没柴烧


qt5.12官方文档: https://doc.qt.io/qt-5/qapplication.html#details ,文档上讲解的比较全面。 下面几个地方需要注意:

1)qApp

A global pointer referring to the unique application object. It is equivalent to QCoreApplication::instance(), but cast as a QApplication pointer, so only valid when the unique application object is a QApplication.

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

2)整个程序中只能有一个QApplication对象!如果程序不是基于Qwideget控件,应该使用QGuiApplication。:

“For any GUI application using Qt, there is precisely one QApplication object, no matter whether the application has 0, 1, 2 or more windows at any given time. For non-QWidget based Qt applications, use QGuiApplication instead, as it does not depend on the QtWidgets library”

3) QApplication's main areas of responsibility are:

  • It initializes the application with the user's desktop settings such as palette(), font() and doubleClickInterval(). 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 using widgetAt(), get a list of topLevelWidgets() and closeAllWindows(), etc.
  • It manages the application's mouse cursor handling, see setOverrideCursor()

看来,该类可以设置整个程序的外观等,功能强大。

4) QApplication 对象要先于其他对象创建
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.(该类需要接受命令行参数)

 

5)该类的一些属性变量:

autoSipEnabled :
Set this property to true to automatically display the SIP when entering widgets that accept keyboard input.

doubleClickInterval : This property holds the time limit in milliseconds that distinguishes a double click from two consecutive mouse clicks

startDragDistance : If you support drag and drop in your application, and want to start a drag and drop operation after the user has moved the cursor a certain distance with a button held down, you should use this property's value as the minimum distance required.

。。。。。。

6)构造函数需要注意
QApplication::QApplication(int &argc, char **argv)

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

Warning: The data referred to by argc and argv must stay valid for the entire lifetime of the QApplication object. 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.

7)事件循环函数:

int QApplication::exec()

Enters the main event loop and waits until exit() is called, then returns the value that was set to exit() (which is 0 if exit() is called via 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 widgets call exec() to start a local event loop.

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

We recommend that you connect clean-up code to the aboutToQuit() signal, instead of putting it in your application's main() function. This is because, on some platforms the QApplication::exec() call may not return. For example, on the Windows platform, 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 QApplication::exec() call.

See also quitOnLastWindowClosed, QCoreApplication::quit(), QCoreApplication::exit(), QCoreApplication::processEvents(), and QCoreApplication::exec().

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
PyQt5是一个Python绑定Qt库的工具包,可以用于创建桌面应用程序。Qt Designer是一个用于创建Qt界面的可视化工具,可以方便地创建GUI界面并导出为.ui文件。 要使用Qt Designer编写PyQt5界面,可以按照以下步骤进行操作: 1. 安装PyQt5和Qt Designer 如果你还没有安装PyQt5和Qt Designer,可以使用以下命令进行安装: ``` pip install PyQt5 pyqt5-tools ``` 2. 创建Qt Designer界面 打开Qt Designer,创建一个新的界面。 3. 设计界面 在Qt Designer中,你可以从工具箱中拖拽控件到界面中,设置控件的属性,布局等。 4. 保存界面 在Qt Designer中,选择“文件”->“保存”,将界面保存为.ui文件。 5. 将.ui文件转换为.py文件 使用以下命令将.ui文件转换为.py文件: ``` pyuic5 -o ui_filename.py ui_filename.ui ``` 其中,ui_filename是你的.ui文件名。这将生成一个.py文件,其中包含Qt Designer界面的Python代码。 6. 编写程序 在Python代码中导入生成的.py文件,然后使用它来创建GUI界面。 下面是一个简单的示例程序: ```python from PyQt5 import QtWidgets, uic class MainWindow(QtWidgets.QMainWindow): def __init__(self): super(MainWindow, self).__init__() # Load the ui file uic.loadUi('ui_filename.ui', self) if __name__ == '__main__': app = QtWidgets.QApplication([]) window = MainWindow() window.show() app.exec_() ``` 在此示例中,我们使用`uic.loadUi`方法将.ui文件加载到`MainWindow`中,然后创建`QApplication`和`MainWindow`实例,并将窗口显示出来。 以上就是使用Qt Designer编写PyQt5界面的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

First Snowflakes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值