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

移动开发
本文介绍的是Qt Designer 布局PyQt学习基础,分为三部分进行介绍,希望友们能深入的去了解,先来看内容。

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

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-07-04 13:17:18

Qt Designer 布局

2011-07-04 13:08:26

Qt Designer

2011-07-04 11:29:40

QT Designer

2011-07-04 11:21:59

QT Designer

2011-07-04 15:43:03

Qt 布局管理器 designer

2011-06-13 14:29:40

Qt Designer

2011-06-10 11:24:08

Qt Quick Designer

2011-06-15 11:09:48

Qt PyQt

2011-06-27 16:07:49

Qt Designer

2011-06-27 16:37:08

Qt Designer

2011-06-28 17:13:46

Qt Designer UI

2011-06-27 16:18:24

Qt Designer

2010-08-05 13:27:06

Flex布局

2011-06-13 15:09:36

插件 Qt Designer

2011-07-04 16:31:24

QT 部件

2011-06-13 14:00:55

Qt Designer linux

2011-06-13 14:49:57

Qt Designer

2011-06-20 15:52:14

Qt Designer 控件

2020-11-09 14:07:53

PyQtQt编程

2011-07-04 14:29:25

Qt Designer 容器
点赞
收藏

51CTO技术栈公众号