这个例子,涉及到书中的源码
文件是两个,一个是findandreplacedlg.py,另一个是findandreplacedlg.ui。findandreplacedlg.py是程序的功能实现部份,findandreplacedlg.ui是由QT designer设计的
界面文件。老规矩,先上原书中的代码:
===============================================================
findandreplacedlg.py
================================================================
import re
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import ui_findandreplacedlg
MAC = True
try:
from PyQt4.QtGui import qt_mac_set_native_menubar
except ImportError:
MAC = False
class FindAndReplaceDlg(QDialog,
ui_findandreplacedlg.Ui_FindAndReplaceDlg):
def __init__(self, text, parent=None):
super(FindAndReplaceDlg, self).__init__(parent)
self.__text = 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()
@pyqtSignature("QString")
def on_findLineEdit_textEdited(self, text):
self.__index = 0
self.updateUi()
def makeRegex(self):
findText = self.findLineEdit.text()
if 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{}\b".format(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(self.replaceLineEdit.text(),
self.__text, 1)
@pyqtSignature("")
def on_replaceAllButton_clicked(self):
regex = self.makeRegex()
self.__text = regex.sub(self.replaceLineEdit.text(),
self.__text)
def updateUi(self):
enable = bool(self.findLineEdit.text())
self.findButton.setEnabled(enable)
self.replaceButton.setEnabled(enable)
self.replaceAllButton.setEnabled(enable)
def text(self):
return self.__text
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 {}".format(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())
====================================================================
findandreplacedlg.ui
====================================================================
<ui version="4.0" >
<class>FindAndReplaceDlg</class>
<widget class="QDialog" name="FindAndReplaceDlg" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>363</width>
<height>192</height>
</rect>
</property>
<property name="windowTitle" >
<string>Find and Replace</string>
</property>
<layout class="QHBoxLayout" >
<property name="margin" >
<number>9</number>
</property>
<property name="spacing" >
<number>6</number>
</property>
<item>
<layout class="QVBoxLayout" >
<property name="margin" >
<number>0</number>
</property>
<property name="spacing" >
<number>6</number>
</property>
<item>
<layout class="QGridLayout" >
<property name="margin" >
<number>0</number>
</property>
<property name="spacing" >
<number>6</number>
</property>
<item row="1" column="1" >
<widget class="QLineEdit" name="replaceLineEdit" />
</item>
<item row="0" column="1" >
<widget class="QLineEdit" name="findLineEdit" />
</item>
<item row="1" column="0" >
<widget class="QLabel" name="label_2" >
<property name="text" >
<string>Replace w&ith:</string>
</property>
<property name="buddy" >
<cstring>replaceLineEdit</cstring>
</property>
</widget>
</item>
<item row="0" column="0" >
<widget class="QLabel" name="label" >
<property name="text" >
<string>Find &what:</string>
</property>
<property name="buddy" >
<cstring>findLineEdit</cstring>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" >
<property name="margin" >
<number>0</number>
</property>
<property name="spacing" >
<number>6</number>
</property>
<item>
<widget class="QCheckBox" name="caseCheckBox" >
<property name="text" >
<string>&Case sensitive</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="wholeCheckBox" >
<property name="text" >
<string>Wh&ole words</string>
</property>
<property name="checked" >
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" >
<property name="margin" >
<number>0</number>
</property>
<property name="spacing" >
<number>6</number>
</property>
<item>
<widget class="QLabel" name="label_3" >
<property name="text" >
<string>&Syntax:</string>
</property>
<property name="buddy" >
<cstring>syntaxComboBox</cstring>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="syntaxComboBox" >
<item>
<property name="text" >
<string>Literal text</string>
</property>
</item>
<item>
<property name="text" >
<string>Regular expression</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
<item>
<spacer>
<property name="orientation" >
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" >
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="Line" name="line" >
<property name="orientation" >
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
<item>
<layout class="QVBoxLayout" >
<property name="margin" >
<number>0</number>
</property>
<property name="spacing" >
<number>6</number>
</property>
<item>
<widget class="QPushButton" name="findButton" >
<property name="text" >
<string>&Find</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="replaceButton" >
<property name="text" >
<string>&Replace</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="replaceAllButton" >
<property name="text" >
<string>Replace &All</string>
</property>
</widget>
</item>
<item>
<spacer>
<property name="orientation" >
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" >
<size>
<width>20</width>
<height>16</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="closeButton" >
<property name="text" >
<string>Close</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<tabstops>
<tabstop>findLineEdit</tabstop>
<tabstop>replaceLineEdit</tabstop>
<tabstop>caseCheckBox</tabstop>
<tabstop>wholeCheckBox</tabstop>
<tabstop>syntaxComboBox</tabstop>
<tabstop>findButton</tabstop>
<tabstop>replaceButton</tabstop>
<tabstop>replaceAllButton</tabstop>
<tabstop>closeButton</tabstop>
</tabstops>
<resources/>
<connections>
<connection>
<sender>closeButton</sender>
<signal>clicked()</signal>
<receiver>FindAndReplaceDlg</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel" >
<x>444</x>
<y>151</y>
</hint>
<hint type="destinationlabel" >
<x>466</x>
<y>170</y>
</hint>
</hints>
</connection>
</connections>
</ui>