Qt的Widgets學習
阿新 • • 發佈:2018-12-12
前言:
所有的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