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学习基础的内容介绍完了,希望本文对你有所帮助!更多内容请参考年纪推荐。

相关内容

热门资讯

PHP新手之PHP入门 PHP是一种易于学习和使用的服务器端脚本语言。只需要很少的编程知识你就能使用PHP建立一个真正交互的...
网络中立的未来 网络中立性是什... 《牛津词典》中对“网络中立”的解释是“电信运营商应秉持的一种原则,即不考虑来源地提供所有内容和应用的...
各种千兆交换机的数据接口类型详... 千兆交换机有很多值得学习的地方,这里我们主要介绍各种千兆交换机的数据接口类型,作为局域网的主要连接设...
什么是大数据安全 什么是大数据... 在《为什么需要大数据安全分析》一文中,我们已经阐述了一个重要观点,即:安全要素信息呈现出大数据的特征...
如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
P2P的自白|我不生产内容,我... 现在一提起P2P,人们就会联想到正在被有关部门“围剿”的互联网理财服务。×租宝事件使得劳...
Intel将Moblin社区控... 本周二,非营利机构Linux基金会宣布,他们将担负起Moblin社区的管理工作,而这之前,Mobli...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...