这是我敲的Python Qt Gui快速编程第六章上的代码,以后可能要用到其中一些,如果忘记了就来这里看看。
#coding=utf-8 #d import os import platform import sys from PyQt4.QtGui import * from PyQt4.QtCore import * __version__= "1.0.0" class MainWindow(QMainWindow): def fileNew(self): print 1 def addActions(self, target,actions): for action in actions: if action is None: target.addSeparator() else : target.addAction(action) def createAction(self,text,slot=None,shortcut=None,icon=None,tip=None, checkable=False,signal="triggered()"): action = QAction(text,self) if icon is not None: action.setIcon(QIcon("web.png")) if shortcut is not None: action.setShortcut(shortcut) if tip is not None: action.setToolTip(tip) action.setStatusTip(tip) if slot is not None: self.connect(action,SIGNAL(signal),slot) if checkable: action.setCheckable(True) return action def __init__(self,parent=None): super(MainWindow, self).__init__(parent) self.image = QImage() self.dirty = False #作为标志用来说明图片是否未保存修改。 self.filename = None #作为标志,用来说明是没有图片还是有尚未保存的新创建图片。 self.mirroredvertically = False #在实现镜像的时候会用到。 self.mirroredhorizontally = False #设置主窗口部件 self.imageLabel = QLabel("123456") self.imageLabel.setMinimumSize(200,200) self.imageLabel.setAlignment(Qt.AlignCenter)#设置图片水平和垂直居中对齐。 self.imageLabel.setContextMenuPolicy(Qt.ActionsContextMenu) #查阅了API文档,明白了 self.setCentralWidget(self.imageLabel) #查阅了 API文档,明白了这个方法可以对主窗口中心区域的窗口部件进行布局,也可以重定义该窗口的父对象 #设置停靠窗口, logDockWidget = QDockWidget("Log", self) #停靠窗口不能放进布局中,所以除了窗口标题外,还要给定父对象。 logDockWidget.setObjectName("LogDockWidget") logDockWidget.setAllowedAreas(Qt.LeftDockWidgetArea|#用这个方法限制其停靠区域 Qt.RightDockWidgetArea) self.listWidget = QListWidget() #创建列表窗口 logDockWidget.setWidget(self.listWidget) #将列表窗口放进停靠窗口中 self.addDockWidget(Qt.RightDockWidgetArea, logDockWidget) #将停靠窗口添加到主窗口中 #设置状态栏信息 self.sizeLabel = QLabel("456") self.sizeLabel.setFrameStyle(QFrame.StyledPanel|QFrame.Sunken) status = self.statusBar() status.setSizeGripEnabled(False)# status.addPermanentWidget(self.sizeLabel,0) #在查阅了API文档之后,明白了这个意思 status.showMessage("Ready",10000) #创建各个动作,File旗下的和Edit旗下的 fileNewAction = self.createAction("&New",self.fileNew,QKeySequence.New,"web","Create an image file") fileOpenAction = self.createAction("&Open",self.fileNew,"Ctrl+O","web","open an image file") editZoomAction = self.createAction("&Zoom",self.fileNew,"Alt+Z","web","Zoom the image") editInvertAction = self.createAction("&Invert",self.fileNew,"Ctrl+I","web","Invert the image's colors",True,"toggled(bool)") editSwapAndBlue = self.createAction("Sw&ap Red and Blue",self.fileNew,"Ctrl+A","web","Swap the colors of red and blue" ,True,"toggled(bool)") mirrorGroup = QActionGroup(self) editUnMirrorAction = self.createAction("&Unmirror",self.fileNew,"Ctrl+U","web","Unmirror the image",True,"toggled(bool)") mirrorHorizon = self.createAction("Mirror &Horizon",self.fileNew,"Ctrl+H","web","Mirror horizon the image", False,"toggled(bool)") mirrorVertical = self.createAction("Mirror &Vertical",self.fileNew,"Ctrl+V","web","Mirror vetrical the image" ,False,"toggled(bool)") mirrorGroup.addAction(editUnMirrorAction) mirrorGroup.addAction(mirrorHorizon) mirrorGroup.addAction(mirrorVertical) menubar = self.menuBar() fileMenu = menubar.addMenu('&File') fileMenu.addAction(fileNewAction) editMenu = menubar.addMenu("&Edit") self.addActions(editMenu,(editInvertAction, editSwapAndBlue,editZoomAction)) mirrorMenu = editMenu.addMenu(QIcon("web"),"&Mirror") self.addActions(mirrorMenu,(editUnMirrorAction,mirrorHorizon,mirrorVertical)) fileToolbar = self.addToolBar("file") #file是这个工具栏的标题 fileToolbar.setObjectName("FileToolBar") #FileToolBar是这个工具栏对象的名字 self.addActions(fileToolbar,(fileNewAction,fileOpenAction)) editToolbar = self.addToolBar("Edit") editToolbar.setObjectName("EditToolBar") self.addActions(editToolbar,(editInvertAction,editSwapAndBlue,editUnMirrorAction, mirrorHorizon,mirrorVertical)) self.zoomSpinBox = QSpinBox() self.zoomSpinBox.setRange(1,400) self.zoomSpinBox.setSuffix("%") self.zoomSpinBox.setValue(100) self.zoomSpinBox.setToolTip("Zoom the image") self.zoomSpinBox.setStatusTip(self.zoomSpinBox.toolTip()) #self.zoomSpinBox.setFocusPolicy(Qt.NoFocus) self.connect(self.zoomSpinBox, SIGNAL("valueChanged(int)"),self.showImage) editToolbar.addWidget(self.zoomSpinBox) self.addActions(self.imageLabel,(editInvertAction, editSwapAndBlue,editUnMirrorAction, mirrorVertical,mirrorHorizon)) def showImage(self): print 2 app = QApplication(sys.argv) mainwindow = MainWindow() mainwindow.show() app.exec_()
1.action = QAction(text,self)-----------第21句
每个QObject子类都要有一个父对象(除了顶级窗口),对于窗口部件来说,它们可以通过布局来获得父对象,而对于纯粹的数据对象比如QAction来说,则必须明确地规定其父对象——self
像Qimage不是QObject的子类,所以可以直接:sekf.image=QImage()--------第36句
2.self.setCentralWidget(self.imageLabel)----------第47句
经参考API文档:The widget argument has it's ownership transferred to Qt.
Sets the given widget to be the main window's central widget.
意思就是括号里面的参数窗口部件的所有权给了MainWindow,就是相当于给它设置了父对象,同时还将imageLabel放在父窗口中间。
3.self.imageLabel.setContextMenuPolicy(Qt.ActionsContextMenu)---------第46句
查得文档,它是这样说的,如果赋予这个参数,那么该窗口将会把该窗口所拥有的动作用在它的右菜单中。
4.logDockWidget = QDockWidget("Log", self) --------------第50句
QdockWidget是停靠窗口类,停靠窗口是不能加入布局的,所以这里需要给它指明父对象。
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句
这个属性保存的是在状态条右下方的QSizeGrip是否有效。
可以让状态条右下方的QSizeGrip生效或者失效。默认它是生效的。
通过setSizeGripEnabled()设置属性值并且通过isSizeGripEnabled()来获得属性值。
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的父对象设置成status,并且如果self.sizeLabel不是QStatusBar的子类的话,那么就重新排列结构。
后面的Int型参数代表占用status bar的相对位置大小,其中permanently意味着该窗口不会被临时信息覆盖,该窗口呆在状态栏最右边。
8.status.showMessage("Ready",10000)-----第64句。
后面的参数表示该信息在状态栏显示的时间,10000ms就是10s。
9.mirrorGroup = QActionGroup(self)-----第73句。
该句创建了一个动作群组的对象,同上,需要给它指定一个父对象。
动作群组可以管理一系列的可选型动作,并可确保它所管理的动作只要有一个是开的,那么其他的动作全为关的。
10.menubar = self.menuBar()----第83句
同创建状态栏一样,第一次调用menuBar的时候也会创建菜单栏。
menuBar()函数属于QMainWindow这个类,其原型为:def menuBar(self),返回值类型为:menuBar(self) -> QMenuBar
statusBar()函数属于QMainWindow这个类,其原型为: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”这个选项。第二句表示给editMenu里面添加一个子菜单。第三句表示给子菜单里面添加动作。一二两句同样是addMenu这个函数,但是所属的类是不同的,第一个函数属于的类是QMenuBar,第二个函数属于QMenu。12.fileToolbar = self.addToolBar("file")-----第92句主窗口里面添加工具栏利用addToolBar这个函数,该句的原型addToolBar(self, QString) -> QToolBar,这个QString是该工具栏的标题
13.fileToolbar.setObjectName("FileToolBar")-----第93句
该句给工具栏对象设置了名字,其作用是帮助PyQt区分多个数量的工具栏就像停靠窗口一样,logDockWidget.setObjectName("LogDockWidget")----第51句
14.self.addActions(editToolbar,(editInvertAction,editSwapAndBlue,editUnMirrorAction,----第97句 mirrorHorizon,mirrorVertical))
给工具栏添加动作。QWidget类都有一个addAction方法,可被QMenu,QMenuBar,QToolBar类继承,这就是为什么可以向这些类添加动作的原因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.
利用完此方法后,该选值框的父对象设定为工具栏,并把该选值框放到工具栏的最后一项。
16.self.addActions(self.imageLabel,(editInvertAction, editSwapAndBlue,editUnMirrorAction, mirrorVertical,mirrorHorizon))
将动作添加到imageLabel中,
self.imageLabel.setContextMenuPolicy(Qt.ActionsContextMenu)----第46句该句的右菜单为该imageLabel的动作。所以在imageLabel中右键,则会弹出该窗口所有的动作指令。另外,只要动作里面没有None就可以这样执行,
因为QWidget类里面没有addSeparator这个方法。