Qt Designer 布局 (3) PyQt學(xué)習(xí)基礎(chǔ)
Qt Designer 布局 (3) PyQt學(xué)習(xí)基礎(chǔ)是本文介紹的內(nèi)容,接著 Qt Designer 布局 (2) PyQt學(xué)習(xí)基礎(chǔ) 文章繼續(xù)了解。我們先來看內(nèi)容。
六,如何在工程中使用
如何使用上面我們產(chǎn)生的py文件呢?首先我們建立一個(gè)findandreplacedlg.py。我們將在這個(gè)文件中使用。
首先是import
- import re
- from PyQt4.QtCore import *
- from PyQt4.QtGui import *
- import ui_findandreplacedlg
- MAC = "qt_mac_set_native_menubar" in dir()
這里的MAC是個(gè)布爾型的,判斷我們的操作系統(tǒng)是否是蘋果系統(tǒng),如果是,MAC=TRUE。
然后我們產(chǎn)生一個(gè)對話框類,注意它的兩個(gè)父類:
- 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繼承生成一個(gè)子類。在初始化函數(shù)__init__中,text參數(shù)是我們需要給這個(gè)窗口的參數(shù),即我們要findandreplace的內(nèi)容,他可以是一個(gè)文件,一段字符串等等。
Super和以前的一樣,對話框初始化,注意是繼承于Qdialog,不是我們在Designer中設(shè)計(jì)的對話框。
self.setupUi(self) 這一句的作用才是把我們在Designer中設(shè)計(jì)的界面搬到我們這個(gè)對話框中,包括它的widgets,tab order等等。這個(gè)setupUi是在ui_findandreplacedlg.py中由pyuic4生成的。
更重要的是,這個(gè)setupUi()方法會(huì)調(diào)用 QtCore.QmetaObject.connectSlotsByName()方法,這個(gè)方法的作用是它會(huì)自動(dòng)創(chuàng)建 signal-slots connection ,這種connection是基于我們子類里的方法,和窗口的widgets之間的,只要我們定義的方法,它的名字是 on_widgetName_signalName的這種格式,就會(huì)自動(dòng)把這個(gè)widgets和這個(gè)signaName connected。
舉個(gè)例子,在我們的form中,我們曾把Find what 這個(gè)Label右邊的 Line Edit這個(gè)widget的ObjectName命名為 findLineEdit ,跟這個(gè)widget相關(guān)的信號,就是這個(gè)Line Edit被編輯了,就會(huì)emit一個(gè)信號 textEdited(Qstring),所以如果我們想綁定這個(gè)widget和這個(gè)signal,就不用調(diào)用connected()方法了,只要我們把方法的名字命名為 on_findLineEdit_textEdited()就可以了,connect的工作就交給setupUi去完成了。
后面的四句setFocusPolicy 是為了方便鍵盤用戶(windows和Linux)的,就是使Tab鍵只能在可編輯widgets之間切換,對于MAC系統(tǒng)不做執(zhí)行。
***的setupUi()方法,是為了在findLineEdit沒有輸入內(nèi)容的時(shí)候,讓幾個(gè)功能按鈕不可用,當(dāng)當(dāng)輸入了以后變?yōu)榭捎谩?/p>
下面是我們定義一些方法:
- @pyqtSignature("QString")
- #確保正確的connect,括號里的參數(shù)為signal的參數(shù)
- def on_findLineEdit_textEdited(self, text):
- self.__index = 0
- self.updateUi()
- #此函數(shù)就是發(fā)現(xiàn)LineEdit有內(nèi)容輸入,buttons變?yōu)榭捎?
- def makeRegex(self):
- #利用正則表達(dá)式來查找內(nèi)容
- 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):
- #返回修改后的結(jié)果
- return self.__text
可以用下面的程序測試一下結(jié)果:
- 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
小結(jié):關(guān)于Qt Designer 布局 (3) PyQt學(xué)習(xí)基礎(chǔ)的內(nèi)容介紹完了,希望本文對你有所幫助!更多內(nèi)容請參考年紀(jì)推薦。