自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Qt Designer 布局 (3) PyQt學(xué)習(xí)基礎(chǔ)

移動(dòng)開發(fā)
本文介紹的是Qt Designer 布局PyQt學(xué)習(xí)基礎(chǔ),分為三部分進(jìn)行介紹,希望友們能深入的去了解,先來看內(nèi)容。

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

  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是個(gè)布爾型的,判斷我們的操作系統(tǒng)是否是蘋果系統(tǒng),如果是,MAC=TRUE。

然后我們產(chǎn)生一個(gè)對話框類,注意它的兩個(gè)父類:

  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繼承生成一個(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>

下面是我們定義一些方法:

  1. @pyqtSignature("QString")   
  2. #確保正確的connect,括號里的參數(shù)為signal的參數(shù)  
  3.     def on_findLineEdit_textEdited(self, text):  
  4.         self.__index = 0 
  5.         self.updateUi()  
  6. #此函數(shù)就是發(fā)現(xiàn)LineEdit有內(nèi)容輸入,buttons變?yōu)榭捎? 
  7.     def makeRegex(self):  
  8.        #利用正則表達(dá)式來查找內(nèi)容  
  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.        #返回修改后的結(jié)果  
  45.         return self.__text 

可以用下面的程序測試一下結(jié)果:

  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

小結(jié):關(guān)于Qt Designer 布局 (3) PyQt學(xué)習(xí)基礎(chǔ)的內(nèi)容介紹完了,希望本文對你有所幫助!更多內(nèi)容請參考年紀(jì)推薦。

責(zé)任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-07-04 13:08:26

Qt Designer

2011-07-04 13:17:18

Qt Designer 布局

2011-07-04 11:21:59

QT Designer

2011-07-04 11:29:40

QT Designer

2011-07-04 15:43:03

Qt 布局管理器 designer

2011-06-15 11:09:48

Qt PyQt

2011-06-13 14:29:40

Qt Designer

2011-06-10 11:24:08

Qt Quick Designer

2011-06-27 16:07:49

Qt Designer

2011-06-27 16:37:08

Qt Designer

2011-07-04 16:31:24

QT 部件

2010-08-05 13:27:06

Flex布局

2011-06-13 15:09:36

插件 Qt Designer

2011-06-28 17:13:46

Qt Designer UI

2011-06-27 16:18:24

Qt Designer

2020-11-09 14:07:53

PyQtQt編程

2011-06-13 14:49:57

Qt Designer

2011-06-13 14:00:55

Qt Designer linux

2011-07-05 15:59:18

Qt 嵌入式 linux

2011-06-27 09:02:18

Qt UDP 網(wǎng)絡(luò)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號