Reputation: 63
I'm studing the PyQt5 library, I found a book ( Rapid GUI Programming with Python and Qt). but in the book the code written in Python 2 and PyQt4. and I'm using Python 3 and PyQt5. this code is from the book, I updated it to fit Python 3. but I still have a problem while running.
import re
import PyQt5, sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import ui_findandreplacedlg
MAC = "qt_mac_set_native_menubar" in dir()
class FindAndReplaceDlg(QDialog, ui_findandreplacedlg.Ui_FindAndReplaceDlg):
def __init__(self,text,parent = None):
super(FindAndReplaceDlg,self).__init__(parent)
self.__text = str(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()
@pyqtSlot("QString")
def on_findLineEdit_textEdited(self, text):
self.__index =0
self.updateUi()
def updateUi(self):
enable = not self.findLineEdit.text().isEmpty()
self.findButton.setEnabled(enable)
self.replaceButton.setEnabled(enable)
self.replaceAllButton.setEnabled(enable)
def text(self):
return self.__text
@pyqtSlot()
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(SIGNSL("found"), match.start())
else:
self.emit(SIGNAL("not found"))
def makeRegex(self):
findText = str(self.on_findLineEdit.text())
if str(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%s\b" % findText
return re.compile(findText, flags)
@pyqtSlot()
def on_replaceButton_clicked(self):
regex = self.makeRegex()
self.__text = regex.sub(str(self.replaceLineEdit.text()), self.__text,1)
@pyqtSlot()
def on_replaceAllButton_clicked(self):
regex = self.makeRegex()
self.__text = regex.sub(str(self.replaceLineEdit.text()),self.__text)
if __name__ == "__main__":
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"),nomore)
form.connect(form, SIGNAL("not found"),found)
form.show()
app.exec_()
print((form.text()))
the first error on this line
def updateUi(self):
enable = not self.findLineEdit.text().isEmpty()
('str' object has no attribute 'isEmpty')
the second error at the end
form = FindAndReplaceDlg(text)
form.connect(form, SIGNAL("found"),nomore)
form.connect(form, SIGNAL("not found"),found)
('FindAndReplaceDlg' has no attribute 'connect') could somebody help me please? and also if you know any good new book to study the Qt Desiner and PyQt5 I'll be also glad!
Upvotes: 1
Views: 688
Reputation: 244003
In PyQt4 the text method of QLineEdit returns a QString but in PyQt5 it returns a str to maintain compatibility with python, so if you want to verify if a string is empty you must use the traditional methods of python. On the other hand in PyQt5 you have to use the new connection syntax, in addition, the dynamic creation of signals is no longer allowed, so you must change it to:
import re
from PyQt5 import QtCore, QtGui, QtWidgets
import ui_findandreplacedlg
MAC = "qt_mac_set_native_menubar" in dir()
class FindAndReplaceDlg(
QtWidgets.QDialog, ui_findandreplacedlg.Ui_FindAndReplaceDlg
):
found = QtCore.pyqtSignal(int)
not_found = QtCore.pyqtSignal()
def __init__(self, text, parent=None):
super(FindAndReplaceDlg, self).__init__(parent)
self.__text = str(text)
self.__index = 0
self.setupUi(self)
if not MAC:
for btn in (
self.findButton,
self.replaceButton,
self.replaceAllButton,
self.closeButton,
):
btn.setFocusPolicy(QtCore.Qt.NoFocus)
self.updateUi()
@QtCore.pyqtSlot(str)
def on_findLineEdit_textEdited(self, text):
self.__index = 0
self.updateUi()
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
@QtCore.pyqtSlot()
def on_findButton_clicked(self):
regex = self.makeRegex()
match = regex.search(self.__text, self.__index)
if match is not None:
self.__index = match.end()
print(match.start())
self.found.emit(match.start())
else:
self.not_found.emit()
def makeRegex(self):
findText = str(self.on_findLineEdit.text())
if str(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%s\b" % findText
return re.compile(findText, flags)
@QtCore.pyqtSlot()
def on_replaceButton_clicked(self):
regex = self.makeRegex()
self.__text = regex.sub(
str(self.replaceLineEdit.text()), self.__text, 1
)
@QtCore.pyqtSlot()
def on_replaceAllButton_clicked(self):
regex = self.makeRegex()
self.__text = regex.sub(str(self.replaceLineEdit.text()), 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 %d" % where))
def nomore():
print("No more found")
app = QtWidgets.QApplication(sys.argv)
form = FindAndReplaceDlg(text)
form.found.connect(nomore)
form.not_found.connect(found)
form.show()
app.exec_()
print((form.text()))
Upvotes: 1