Qt Designer 布局 (3) PyQt学习基础
创始人
2024-08-02 11:41:11
0

Qt Designer 布局 (3) PyQt学习基础是本文介绍的内容,接着 Qt Designer 布局 (2) PyQt学习基础 文章继续了解。我们先来看内容。

六,如何在工程中使用

如何使用上面我们产生的py文件呢?首先我们建立一个findandreplacedlg.py。我们将在这个文件中使用。

首先是import

  1. import re  
  2. from PyQt4.QtCore import *  
  3. from PyQt4.QtGui import *  
  4. import ui_findandreplacedlg  
  5. MAC = "qt_mac_set_native_menubar" in dir() 

这里的MAC是个布尔型的,判断我们的操作系统是否是苹果系统,如果是,MAC=TRUE。

然后我们产生一个对话框类,注意它的两个父类:

  1. class FindAndReplaceDlg(QDialog,  
  2.         ui_findandreplacedlg.Ui_FindAndReplaceDlg):  
  3.     def __init__(self, text, parent=None):  
  4.         super(FindAndReplaceDlg, self).__init__(parent)  
  5.         self.__text = unicode(text)  
  6.         self.__index = 0 
  7.         self.setupUi(self)  
  8.         if not MAC:  
  9.             self.findButton.setFocusPolicy(Qt.NoFocus)  
  10.             self.replaceButton.setFocusPolicy(Qt.NoFocus)  
  11.             self.replaceAllButton.setFocusPolicy(Qt.NoFocus)  
  12.             self.closeButton.setFocusPolicy(Qt.NoFocus)  
  13.         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没有输入内容的时候,让几个功能按钮不可用,当当输入了以后变为可用。

下面是我们定义一些方法:

  1. @pyqtSignature("QString")   
  2. #确保正确的connect,括号里的参数为signal的参数  
  3.     def on_findLineEdit_textEdited(self, text):  
  4.         self.__index = 0 
  5.         self.updateUi()  
  6. #此函数就是发现LineEdit有内容输入,buttons变为可用  
  7.     def makeRegex(self):  
  8.        #利用正则表达式来查找内容  
  9.         findText = unicode(self.findLineEdit.text())  
  10.         if unicode(self.syntaxComboBox.currentText()) == "Literal":  
  11.             findText = re.escape(findText)  
  12.             flags = re.MULTILINE|re.DOTALL|re.UNICODE  
  13.         if not self.caseCheckBox.isChecked():  
  14.             flags |= re.IGNORECASE  
  15.         if self.wholeCheckBox.isChecked():  
  16.             findText = r"b%sb" % findText  
  17.         return re.compile(findText, flags)  
  18.     @pyqtSignature("")  
  19.     def on_findButton_clicked(self):  
  20.         regex = self.makeRegex()  
  21.         match = regex.search(self.__text, self.__index)  
  22.         if match is not None:  
  23.             self.__index = match.end()  
  24.             self.emit(SIGNAL("found"), match.start())  
  25.         else:  
  26.             self.emit(SIGNAL("notfound")            
  27.     @pyqtSignature("")  
  28.     def on_replaceButton_clicked(self):  
  29.         regex = self.makeRegex()  
  30.         self.__text = regex.sub(unicode(self.replaceLineEdit.text()),  
  31.                                 self.__text, 1)       
  32.     @pyqtSignature("")  
  33.     def on_replaceAllButton_clicked(self):  
  34.         regex = self.makeRegex()  
  35.         self.__text = regex.sub(unicode(self.replaceLineEdit.text()),  
  36.                                 self.__text)   
  37.     def updateUi(self):  
  38.  #判断LineEdit是否为空,从而决定buttons是否可用  
  39.         enable = not self.findLineEdit.text().isEmpty()  
  40.         self.findButton.setEnabled(enable)  
  41.         self.replaceButton.setEnabled(enable)  
  42.         self.replaceAllButton.setEnabled(enable)  
  43.     def text(self):  
  44.        #返回修改后的结果  
  45.         return self.__text 

可以用下面的程序测试一下结果:

  1. if __name__ == "__main__":  
  2.     import sys  
  3.     text = """US experience shows that, unlike traditional patents,  
  4. software patents do not encourage innovation and R&D, quite the  
  5. contrary. In particular they hurt small and medium-sized enterprises  
  6. and generally newcomers in the market. They will just weaken the market  
  7. and increase spending on patents and litigation, at the expense of  
  8. technological innovation and research. Especially dangerous are  
  9. attempts to abuse the patent system by preventing interoperability as a  
  10. means of avoiding competition with technological ability.  
  11. --- Extract quoted from Linus Torvalds and Alan Cox's letter  
  12. to the President of the European Parliament  
  13. http://www.effi.org/patentit/patents_torvalds_cox.html"""  
  14.     def found(where):  
  15.         print "Found at %d" % where  
  16.     def nomore():  
  17.         print "No more found"  
  18.     app = QApplication(sys.argv)  
  19.     form = FindAndReplaceDlg(text)  
  20.     form.connect(form, SIGNAL("found"), found)  
  21.     form.connect(form, SIGNAL("notfound"), nomore)  
  22.     form.show()  
  23.     app.exec_()  
  24.     print form.text() 

参考资料《Rapid GUI Programing with PyQt》chapter 7

小结:关于Qt Designer 布局 (3) PyQt学习基础的内容介绍完了,希望本文对你有所帮助!更多内容请参考年纪推荐。

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...