Python Qt GUI快速编程第六章代码分析
1.action = QAction(text,self)-----------第21句 每個(gè)QObject子類都要有一個(gè)父對(duì)象(除了頂級(jí)窗口),對(duì)于窗口部件來(lái)說(shuō),它們可以通過(guò)布局來(lái)獲得父對(duì)象,而對(duì)于純粹的數(shù)據(jù)對(duì)象比如QAction來(lái)說(shuō),則必須明確地規(guī)定其父對(duì)象——self 像Qimage不是QObject的子類,所以可以直接:sekf.image=QImage()--------第36句 2.self.setCentralWidget(self.imageLabel)----------第47句 經(jīng)參考API文檔:
The widget argument has it's ownership transferred to Qt.
Sets the given widget to be the main window's central widget.
意思就是括號(hào)里面的參數(shù)窗口部件的所有權(quán)給了MainWindow,就是相當(dāng)于給它設(shè)置了父對(duì)象,同時(shí)還將imageLabel放在父窗口中間。
3.self.imageLabel.setContextMenuPolicy(Qt.ActionsContextMenu)---------第46句
查得文檔,它是這樣說(shuō)的,如果賦予這個(gè)參數(shù),那么該窗口將會(huì)把該窗口所擁有的動(dòng)作用在它的右菜單中。
4.logDockWidget = QDockWidget("Log", self) --------------第50句
QdockWidget是停靠窗口類,停靠窗口是不能加入布局的,所以這里需要給它指明父對(duì)象。
5.logDockWidget.setWidget(self.listWidget)-------第55句self.addDockWidget(Qt.RightDockWidgetArea, logDockWidget)-------第56句給停靠窗口里面添加窗口需要用setWidget(),其原型為:def setWidget(self, QWidget)給主窗口里面添加停靠窗口需要用addDockWidget,其原型為:def addDockWidget(self, Qt_DockWidgetArea, QDockWidget, Qt_Orientation=None) 6.status.setSizeGripEnabled(False)--------第62句
這個(gè)屬性保存的是在狀態(tài)條右下方的QSizeGrip是否有效。
可以讓狀態(tài)條右下方的QSizeGrip生效或者失效。默認(rèn)它是生效的。
通過(guò)setSizeGripEnabled()設(shè)置屬性值并且通過(guò)isSizeGripEnabled()來(lái)獲得屬性值。
7.status.addPermanentWidget(self.sizeLabel,0)-------第63句
查閱參考文檔:
The widget argument has it's ownership transferred to Qt.
Adds the given widget permanently to this status bar, reparenting the widget if it isn't already a child of this QStatusBar object. The stretch parameter is used to compute a suitable size for the given widget as the status bar grows and shrinks. The default stretch factor is 0, i.e giving the widget a minimum of space.
Permanently means that the widget may not be obscured by temporary messages. It is is located at the far right of the status bar.
意思就是將self.sizeLabel的父對(duì)象設(shè)置成status,并且如果self.sizeLabel不是QStatusBar的子類的話,那么就重新排列結(jié)構(gòu)。
后面的Int型參數(shù)代表占用status bar的相對(duì)位置大小,其中permanently意味著該窗口不會(huì)被臨時(shí)信息覆蓋,該窗口呆在狀態(tài)欄最右邊。
8.status.showMessage("Ready",10000)-----第64句。
后面的參數(shù)表示該信息在狀態(tài)欄顯示的時(shí)間,10000ms就是10s。
9.mirrorGroup = QActionGroup(self)-----第73句。
該句創(chuàng)建了一個(gè)動(dòng)作群組的對(duì)象,同上,需要給它指定一個(gè)父對(duì)象。
動(dòng)作群組可以管理一系列的可選型動(dòng)作,并可確保它所管理的動(dòng)作只要有一個(gè)是開的,那么其他的動(dòng)作全為關(guān)的。
10.menubar = self.menuBar()----第83句
同創(chuàng)建狀態(tài)欄一樣,第一次調(diào)用menuBar的時(shí)候也會(huì)創(chuàng)建菜單欄。
menuBar()函數(shù)屬于QMainWindow這個(gè)類,其原型為:def menuBar(self),返回值類型為:menuBar(self) -> QMenuBar
statusBar()函數(shù)屬于QMainWindow這個(gè)類,其原型為:def statusBar(self),返回值類型為:
statusBar(self) -> QStatusBar 11.editMenu = menubar.addMenu("&Edit")----第86句mirrorMenu = editMenu.addMenu(QIcon("web"),"&Mirror")-----第89句self.addActions(mirrorMenu,(editUnMirrorAction,mirrorHorizon,mirrorVertical))---第90句第一句表示給主菜單里面添加“Edit”這個(gè)選項(xiàng)。第二句表示給editMenu里面添加一個(gè)子菜單。 第三句表示給子菜單里面添加動(dòng)作。一二兩句同樣是addMenu這個(gè)函數(shù),但是所屬的類是不同的,第一個(gè)函數(shù)屬于的類是QMenuBar,第二個(gè)函數(shù)屬于QMenu。 12.fileToolbar = self.addToolBar("file")-----第92句 主窗口里面添加工具欄利用addToolBar這個(gè)函數(shù), 該句的原型addToolBar(self, QString) -> QToolBar,這個(gè)QString是該工具欄的標(biāo)題 13.fileToolbar.setObjectName("FileToolBar")-----第93句該句給工具欄對(duì)象設(shè)置了名字,其作用是幫助PyQt區(qū)分多個(gè)數(shù)量的工具欄就像停靠窗口一樣,logDockWidget.setObjectName("LogDockWidget")----第51句 14.self.addActions(editToolbar,(editInvertAction,editSwapAndBlue,editUnMirrorAction,----第97句mirrorHorizon,mirrorVertical))給工具欄添加動(dòng)作。QWidget類都有一個(gè)addAction方法,可被QMenu,QMenuBar,QToolBar類繼承,這就是為什么可以向這些類添加動(dòng)作的原因 15.editToolbar.addWidget(self.zoomSpinBox)----109句查閱文檔:The widget argument has it's ownership transferred to Qt.
Adds the given widget to the toolbar as the toolbar's last item.
The toolbar takes ownership of widget.
利用完此方法后,該選值框的父對(duì)象設(shè)定為工具欄,并把該選值框放到工具欄的最后一項(xiàng)。
16.self.addActions(self.imageLabel,(editInvertAction,editSwapAndBlue,editUnMirrorAction,mirrorVertical,mirrorHorizon))
將動(dòng)作添加到imageLabel中,
self.imageLabel.setContextMenuPolicy(Qt.ActionsContextMenu)----第46句該句的右菜單為該imageLabel的動(dòng)作。所以在imageLabel中右鍵,則會(huì)彈出該窗口所有的動(dòng)作指令。另外,只要?jiǎng)幼骼锩鏇]有None就可以這樣執(zhí)行,
因?yàn)镼Widget類里面沒有addSeparator這個(gè)方法。
與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖
總結(jié)
以上是生活随笔為你收集整理的Python Qt GUI快速编程第六章代码分析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: PyQt编程之模态与非模态对话框(二)
- 下一篇: PyQt4编程之简短地做出多个选择框