李程站 发表于 2013-2-4 03:00:56

Qt Designer 布局 (3) PyQt学习基础

Qt Designer 布局 (3) PyQt学习基础是本文介绍的内容,接着 Qt Designer 布局 (2) PyQt学习基础 文章继续了解。我们先来看内容。
六,如何在工程中使用
如何使用上面我们产生的py文件呢?首先我们建立一个findandreplacedlg.py。我们将在这个文件中使用。
首先是import

[*]import re
[*]from PyQt4.QtCore import *
[*]from PyQt4.QtGui import *
[*]import ui_findandreplacedlg
[*]MAC = "qt_mac_set_native_menubar" in dir()
这里的MAC是个布尔型的,判断我们的操作系统是否是苹果系统,如果是,MAC=TRUE。
然后我们产生一个对话框类,注意它的两个父类:

[*]class FindAndReplaceDlg(QDialog,
[*]      ui_findandreplacedlg.Ui_FindAndReplaceDlg):
[*]    def __init__(self, text, parent=None):
[*]      super(FindAndReplaceDlg, self).__init__(parent)
[*]      self.__text = unicode(text)
[*]      self.__index = 0
[*]      self.setupUi(self)
[*]      if not MAC:
[*]            self.findButton.setFocusPolicy(Qt.NoFocus)
[*]            self.replaceButton.setFocusPolicy(Qt.NoFocus)
[*]            self.replaceAllButton.setFocusPolicy(Qt.NoFocus)
[*]            self.closeButton.setFocusPolicy(Qt.NoFocus)
[*]      self.updateUi()
我们从Qdialod和ui_findandreplacedlg.Ui_FindAndReplaceDlg继承生成一个子类。在初始化函数__init__中,text参数是我们需要给这个窗口的参数,即我们要findandreplace的内容,他可以是一个文件,一段字符串等等。
Super和以前的一样,对话框初始化,注意是继承于Qdialog,不是我们在Designer中设计的对话框。
self.setupUi(self) 这一句的作用才是把我们在Designer中设计的界面搬到我们这个对话框中,包括它的widgets,tab order等等。这个setupUi是在ui_findandreplacedlg.py中由pyuic4生成的。
更重要的是,这个setupUi()方法会调用 QtCore.QmetaObject.connectSlotsByName()方法,这个方法的作用是它会自动创建 signal-slots connection ,这种connection是基于我们子类里的方法,和窗口的widgets之间的,只要我们定义的方法,它的名字是 on_widgetName_signalName的这种格式,就会自动把这个widgets和这个signaName connected。
举个例子,在我们的form中,我们曾把Find what 这个Label右边的 Line Edit这个widget的ObjectName命名为 findLineEdit ,跟这个widget相关的信号,就是这个Line Edit被编辑了,就会emit一个信号 textEdited(Qstring),所以如果我们想绑定这个widget和这个signal,就不用调用connected()方法了,只要我们把方法的名字命名为 on_findLineEdit_textEdited()就可以了,connect的工作就交给setupUi去完成了。
后面的四句setFocusPolicy 是为了方便键盘用户(windows和Linux)的,就是使Tab键只能在可编辑widgets之间切换,对于MAC系统不做执行。
最后的setupUi()方法,是为了在findLineEdit没有输入内容的时候,让几个功能按钮不可用,当当输入了以后变为可用。
下面是我们定义一些方法:

[*]@pyqtSignature("QString")   
[*]#确保正确的connect,括号里的参数为signal的参数
[*]    def on_findLineEdit_textEdited(self, text):
[*]      self.__index = 0
[*]      self.updateUi()
[*]#此函数就是发现LineEdit有内容输入,buttons变为可用
[*]    def makeRegex(self):
[*]       #利用正则表达式来查找内容
[*]      findText = unicode(self.findLineEdit.text())
[*]      if unicode(self.syntaxComboBox.currentText()) == "Literal":
[*]            findText = re.escape(findText)
[*]            flags = re.MULTILINE|re.DOTALL|re.UNICODE
[*]      if not self.caseCheckBox.isChecked():
[*]            flags |= re.IGNORECASE
[*]      if self.wholeCheckBox.isChecked():
[*]            findText = r"b%sb" % findText
[*]      return re.compile(findText, flags)
[*]    @pyqtSignature("")
[*]    def on_findButton_clicked(self):
[*]      regex = self.makeRegex()
[*]      match = regex.search(self.__text, self.__index)
[*]      if match is not None:
[*]            self.__index = match.end()
[*]            self.emit(SIGNAL("found"), match.start())
[*]      else:
[*]            self.emit(SIGNAL("notfound")            
[*]    @pyqtSignature("")
[*]    def on_replaceButton_clicked(self):
[*]      regex = self.makeRegex()
[*]      self.__text = regex.sub(unicode(self.replaceLineEdit.text()),
[*]                              self.__text, 1)      
[*]    @pyqtSignature("")
[*]    def on_replaceAllButton_clicked(self):
[*]      regex = self.makeRegex()
[*]      self.__text = regex.sub(unicode(self.replaceLineEdit.text()),
[*]                              self.__text)   
[*]    def updateUi(self):
[*] #判断LineEdit是否为空,从而决定buttons是否可用
[*]      enable = not self.findLineEdit.text().isEmpty()
[*]      self.findButton.setEnabled(enable)
[*]      self.replaceButton.setEnabled(enable)
[*]      self.replaceAllButton.setEnabled(enable)
[*]    def text(self):
[*]       #返回修改后的结果
[*]      return self.__text
可以用下面的程序测试一下结果:

[*]if __name__ == "__main__":
[*]    import sys
[*]    text = """US experience shows that, unlike traditional patents,
[*]software patents do not encourage innovation and R&D, quite the
[*]contrary. In particular they hurt small and medium-sized enterprises
[*]and generally newcomers in the market. They will just weaken the market
[*]and increase spending on patents and litigation, at the expense of
[*]technological innovation and research. Especially dangerous are
[*]attempts to abuse the patent system by preventing interoperability as a
[*]means of avoiding competition with technological ability.
[*]--- Extract quoted from Linus Torvalds and Alan Cox's letter
[*]to the President of the European Parliament
[*]http://www.effi.org/patentit/patents_torvalds_cox.html"""
[*]    def found(where):
[*]      print "Found at %d" % where
[*]    def nomore():
[*]      print "No more found"
[*]    app = QApplication(sys.argv)
[*]    form = FindAndReplaceDlg(text)
[*]    form.connect(form, SIGNAL("found"), found)
[*]    form.connect(form, SIGNAL("notfound"), nomore)
[*]    form.show()
[*]    app.exec_()
[*]    print form.text()
参考资料《Rapid GUI Programing with PyQt》chapter 7
小结:关于Qt Designer 布局 (3) PyQt学习基础的内容介绍完了,希望本文对你有所帮助!更多内容请参考年纪推荐。
【编辑推荐】

[*]快速开发交叉编译之Qt Designer
[*]Qt 创建自定义部件集成到Qt Designer中
[*]Qt Designer教程 给自己编槽
[*]Qt Designer 教程实例调用退出
[*]Qt Designer设计 UI 文件并调用
[*]QT Designer 入门学习
页: [1]
查看完整版本: Qt Designer 布局 (3) PyQt学习基础