Qt的Widgets学习

前言:

所有的QT类都继承与QObject,这个包括所有的widgets。因为QWidget是QObject的子类。如果说一个widget没有父类,则我们称它为top-level window

任何widget都能被用作top-level window,但是在大多数时候,我们创建一个top-level window继承与QDialog或者QMainWindow或者QWidget,事实上QDialog和QMainWindow继承与QWidget。

一个有父类的widget,被包含在它父亲的容器当中,这种类的父系关系在qt中被称为所属关系。

如果一个widget没有一个父亲的话,则在GUI中它就无法显示(当然如果一个没有父类的widget:A用其他widget:B的add加入到它的容器当中,也相当于这个widget:A有了一个父亲widget:B)

QWidget

最基础的抽象

  • QDialog
    继承于QWidget,没有menubar和statusbar
  • ==QMainWindow ==
    继承于QWidget,有menubar和statusbar,可以依据需要删除

Ui_QDialog()与Ui_MainWindow()


  • 这两个类只是在Qt Designer与PyQt中起一个巧妙的承接作用。首先它们自己的setupUi函数会将最根本的那个根QWidget传进来,然后让所有在Qt Designer中添加生成的widget继承于这个根QWidget,然后再设置widget相关的属性。
  • 例如在python的call_Ui程序中,编写一个主窗口抽象时,首先是继承QDialog或者QMainWindow,然后将自己本身抽象self传入setupUi()函数,作为在Qt Designer中生成窗口部件的根Qwidget。

show( )方法


Qt5文档:

  • show()
    Shows the widget and its child widgets.
    This is equivalent to calling showFullScreen(), showMaximized(), or setVisible(true), depending on the platform’s default behavior for the window flags.
    调用show()方法后,显示这个窗口部件,还有它全部的子窗口部件。
    这个函数和showFullScreen(), showMaximized(), or setVisible(true)这些函数等价!
  • setVisible()
    Calling setVisible(true) or show() sets the widget to visible status if all its parent widgets up to the window are visible. If an ancestor is not visible, the widget won’t become visible until all its ancestors are shown.
    调用此方法使得这个窗口部件可见,前提是它的父类的窗口部件全部都可见。如果有一个窗口部件不可见,则它也不可见。
  • Once we have set up the label that will be our window, we call show() on it. At this point, the label window is not shown! The call to show() merely schedules a “paint event”, that is, it adds a new event to the QApplication object’s event queue that is a request to paint the specified widget.
    一旦我们创建了一个标签作为我们的窗口,我们就可以调用show()函数。这时,窗口并没有出现,show()函数的调用仅仅安排了一个paint event ,这个事件添加到QApplication对象的事件队列中,添加的事件就是去绘制一个具体的窗口部件。

QApplication.exec()函数


  • The following line makes the QApplication enter its event loop. When a Qt application is running, events are generated and sent to the widgets of the application. Examples of events are mouse presses and key strokes.
    QApplication.exec()函数调用后,进入事件循环。当一个Qt应用运行时,事件将被产生然后被发送到应用的窗口部件去。
  • 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.
    调用这个函数用来开始事件处理,这个主事件循环接收从窗口系统发来的事件,然后将这些事件分配给应用的窗口部件。
  • When the user interacts with the application, or when certain other things
    occur, such as a timer timing out or the application’s window being uncovered
    (maybe because another application was closed), an event is generated inside
    PyQt and added to the event queue
    .
    当用户与应用交互时,或者当确定的事件发生时例如一个timer定时器,或者我们的窗口没有被覆盖时,一个事件就会自动的添加到主事件队列中去。
  • Once invoked, they run their event loop and respond to events. Some events come from the user—for example, key presses and mouse clicks—and some from the system, for example, timers timing out and windows being revealed.
    一旦应用被激活,它们将运行事件循环,然后对事件作出回应。一些事件来自于用户,比如鼠标和按键,另一些来自于系统,比如定时器等

QApplication loop 循环

Event loops are used by all GUI applications. In pseudocode, an event loop looks like this:

while True:
	event = getNextEvent()
	if event:
		if event == Terminate:
			break
		processEvent(event)

流程图
在这里插入图片描述
事件分类:

  • QPaintEvent():Paint events are sent to widgets that need to update themselves, for instance when part of a widget is exposed because a covering widget was moved.
  • QShowEvent( ): The QShowEvent class provides an event that is sent when a widget is shown.
  • key presses and mouse clicks
  • timers timing out and windows being revealed

猜你喜欢

转载自blog.csdn.net/qq_28485501/article/details/84783771