• 1104阅读
  • 8回复

简析第7章“使用QT设计师”的例子"查找和替换对话框" [复制链接]

上一主题 下一主题
离线wps2000
 

只看楼主 倒序阅读 楼主  发表于: 2017-06-22
这个例子,涉及到书中的源码文件是两个,一个是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>

离线wps2000

只看该作者 1楼 发表于: 2017-06-22
需要说明的是:传统的GUI程序开发不区分前端和后台,统一使用代码管理,源文件中既有创建和设置控件的代码,又有处理业务逻辑的代码,非常杂乱。而.ui文件是 Qt Designer 的界面设计文件,由 XML 代码构成。可以使业务逻辑与界面的分离。但在使用界面代码之前,需将.ui文件转换成.py格式的python源码。在书中,用的是:pyuic4 -o ui_findandreplacedlg.py findandreplacedlg.ui。而在QT5模式下,要将pyuic4换成pyuic5。因此,转换命令如下:
pyuic5 -o ui_findandreplacedlg.py findandreplacedlg.ui

-o参数后面,ui_findandreplacedlg.py是python格式的目标文件,findandreplacedlg.ui是格式的界面文件。转化后的代码如下:


===================================================================
ui_findandreplacedlg.py
===================================================================



from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_FindAndReplaceDlg(object):
    def setupUi(self, FindAndReplaceDlg):
        FindAndReplaceDlg.setObjectName("FindAndReplaceDlg")
        FindAndReplaceDlg.resize(363, 192)
        self.hboxlayout = QtWidgets.QHBoxLayout(FindAndReplaceDlg)
        self.hboxlayout.setContentsMargins(9, 9, 9, 9)
        self.hboxlayout.setSpacing(6)
        self.hboxlayout.setObjectName("hboxlayout")
        self.vboxlayout = QtWidgets.QVBoxLayout()
        self.vboxlayout.setContentsMargins(0, 0, 0, 0)
        self.vboxlayout.setSpacing(6)
        self.vboxlayout.setObjectName("vboxlayout")
        self.gridlayout = QtWidgets.QGridLayout()
        self.gridlayout.setContentsMargins(0, 0, 0, 0)
        self.gridlayout.setSpacing(6)
        self.gridlayout.setObjectName("gridlayout")
        self.replaceLineEdit = QtWidgets.QLineEdit(FindAndReplaceDlg)
        self.replaceLineEdit.setObjectName("replaceLineEdit")
        self.gridlayout.addWidget(self.replaceLineEdit, 1, 1, 1, 1)
        self.findLineEdit = QtWidgets.QLineEdit(FindAndReplaceDlg)
        self.findLineEdit.setObjectName("findLineEdit")
        self.gridlayout.addWidget(self.findLineEdit, 0, 1, 1, 1)
        self.label_2 = QtWidgets.QLabel(FindAndReplaceDlg)
        self.label_2.setObjectName("label_2")
        self.gridlayout.addWidget(self.label_2, 1, 0, 1, 1)
        self.label = QtWidgets.QLabel(FindAndReplaceDlg)
        self.label.setObjectName("label")
        self.gridlayout.addWidget(self.label, 0, 0, 1, 1)
        self.vboxlayout.addLayout(self.gridlayout)
        self.hboxlayout1 = QtWidgets.QHBoxLayout()
        self.hboxlayout1.setContentsMargins(0, 0, 0, 0)
        self.hboxlayout1.setSpacing(6)
        self.hboxlayout1.setObjectName("hboxlayout1")
        self.caseCheckBox = QtWidgets.QCheckBox(FindAndReplaceDlg)
        self.caseCheckBox.setObjectName("caseCheckBox")
        self.hboxlayout1.addWidget(self.caseCheckBox)
        self.wholeCheckBox = QtWidgets.QCheckBox(FindAndReplaceDlg)
        self.wholeCheckBox.setChecked(True)
        self.wholeCheckBox.setObjectName("wholeCheckBox")
        self.hboxlayout1.addWidget(self.wholeCheckBox)
        self.vboxlayout.addLayout(self.hboxlayout1)
        self.hboxlayout2 = QtWidgets.QHBoxLayout()
        self.hboxlayout2.setContentsMargins(0, 0, 0, 0)
        self.hboxlayout2.setSpacing(6)
        self.hboxlayout2.setObjectName("hboxlayout2")
        self.label_3 = QtWidgets.QLabel(FindAndReplaceDlg)
        self.label_3.setObjectName("label_3")
        self.hboxlayout2.addWidget(self.label_3)
        self.syntaxComboBox = QtWidgets.QComboBox(FindAndReplaceDlg)
        self.syntaxComboBox.setObjectName("syntaxComboBox")
        self.syntaxComboBox.addItem("")
        self.syntaxComboBox.addItem("")
        self.hboxlayout2.addWidget(self.syntaxComboBox)
        self.vboxlayout.addLayout(self.hboxlayout2)
        spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.vboxlayout.addItem(spacerItem)
        self.hboxlayout.addLayout(self.vboxlayout)
        self.line = QtWidgets.QFrame(FindAndReplaceDlg)
        self.line.setFrameShape(QtWidgets.QFrame.VLine)
        self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line.setObjectName("line")
        self.hboxlayout.addWidget(self.line)
        self.vboxlayout1 = QtWidgets.QVBoxLayout()
        self.vboxlayout1.setContentsMargins(0, 0, 0, 0)
        self.vboxlayout1.setSpacing(6)
        self.vboxlayout1.setObjectName("vboxlayout1")
        self.findButton = QtWidgets.QPushButton(FindAndReplaceDlg)
        self.findButton.setObjectName("findButton")
        self.vboxlayout1.addWidget(self.findButton)
        self.replaceButton = QtWidgets.QPushButton(FindAndReplaceDlg)
        self.replaceButton.setObjectName("replaceButton")
        self.vboxlayout1.addWidget(self.replaceButton)
        self.replaceAllButton = QtWidgets.QPushButton(FindAndReplaceDlg)
        self.replaceAllButton.setObjectName("replaceAllButton")
        self.vboxlayout1.addWidget(self.replaceAllButton)
        spacerItem1 = QtWidgets.QSpacerItem(20, 16, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.vboxlayout1.addItem(spacerItem1)
        self.closeButton = QtWidgets.QPushButton(FindAndReplaceDlg)
        self.closeButton.setObjectName("closeButton")
        self.vboxlayout1.addWidget(self.closeButton)
        self.hboxlayout.addLayout(self.vboxlayout1)
        self.label_2.setBuddy(self.replaceLineEdit)
        self.label.setBuddy(self.findLineEdit)
        self.label_3.setBuddy(self.syntaxComboBox)

        self.retranslateUi(FindAndReplaceDlg)
        self.closeButton.clicked.connect(FindAndReplaceDlg.reject)
        QtCore.QMetaObject.connectSlotsByName(FindAndReplaceDlg)
        FindAndReplaceDlg.setTabOrder(self.findLineEdit, self.replaceLineEdit)
        FindAndReplaceDlg.setTabOrder(self.replaceLineEdit, self.caseCheckBox)
        FindAndReplaceDlg.setTabOrder(self.caseCheckBox, self.wholeCheckBox)
        FindAndReplaceDlg.setTabOrder(self.wholeCheckBox, self.syntaxComboBox)
        FindAndReplaceDlg.setTabOrder(self.syntaxComboBox, self.findButton)
        FindAndReplaceDlg.setTabOrder(self.findButton, self.replaceButton)
        FindAndReplaceDlg.setTabOrder(self.replaceButton, self.replaceAllButton)
        FindAndReplaceDlg.setTabOrder(self.replaceAllButton, self.closeButton)

    def retranslateUi(self, FindAndReplaceDlg):
        _translate = QtCore.QCoreApplication.translate
        FindAndReplaceDlg.setWindowTitle(_translate("FindAndReplaceDlg", "Find and Replace"))
        self.label_2.setText(_translate("FindAndReplaceDlg", "Replace w&ith:"))
        self.label.setText(_translate("FindAndReplaceDlg", "Find &what:"))
        self.caseCheckBox.setText(_translate("FindAndReplaceDlg", "&Case sensitive"))
        self.wholeCheckBox.setText(_translate("FindAndReplaceDlg", "Wh&ole words"))
        self.label_3.setText(_translate("FindAndReplaceDlg", "&Syntax:"))
        self.syntaxComboBox.setItemText(0, _translate("FindAndReplaceDlg", "Literal text"))
        self.syntaxComboBox.setItemText(1, _translate("FindAndReplaceDlg", "Regular expression"))
        self.findButton.setText(_translate("FindAndReplaceDlg", "&Find"))
        self.replaceButton.setText(_translate("FindAndReplaceDlg", "&Replace"))
        self.replaceAllButton.setText(_translate("FindAndReplaceDlg", "Replace &All"))
        self.closeButton.setText(_translate("FindAndReplaceDlg", "Close"))
离线wps2000

只看该作者 2楼 发表于: 2017-06-22
得到ui_findandreplacedlg.py文件之后,就可以在业务逻辑文件findandreplacedlg.py中,将ui_findandreplacedlg.py作为模块引入:


import ui_findandreplacedlg


只需将名称ui_findandreplacedlg引入,不要引入全文件名。


此外,在findandreplacedlg.py中,依次将原有的涉及到的QT4模块变为QT5的模块,同时保留正则表达式处理模块re:



import re
from PyQt5.QtCore import pyqtSlot,pyqtSignal,Qt
from PyQt5.QtWidgets import QDialog,QApplication


在原来的代码中,还有一段:

MAC = True
try:
    from PyQt4.QtGui import qt_mac_set_native_menubar
except ImportError:
    MAC = False


这是对MAC系统的检查,要改为:
MAC = "qt_mac_set_native_menubar" in dir()

离线wps2000

只看该作者 3楼 发表于: 2017-06-22
在文件findandreplacedlg.py中,所有涉及到@pyqtSignature的装饰器修饰,统统改成pyqtSlot。pyqtSignature是旧式风格,在QT5成为了pyqtSlot。pyqtSlot是对槽的修饰,使用的目的是为了区分各个具有同样名字不同参数的信号(原书中P163注)。



在类FindAndReplaceDlg中,on_findButton_clicked方法,涉及到了两个信号:found和notfound:

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"))

这两个信号,都是通过emit来发射,而在python3.x中,信号必须事先定义:

notfound = pyqtSignal()
found = pyqtSignal(int)
可以将这两个信号的定义,放在类FindAndReplaceDlg的里面:
================================================================

class FindAndReplaceDlg(QDialog,ui_findandreplacedlg.Ui_FindAndReplaceDlg):
    notfound = pyqtSignal()
    found = pyqtSignal(int)
==================================================================
然后,在方法on_findButton_clicked中,改成python3.x的发射方式:



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())
            self.found.emit(match.start())
        else:
            #self.emit(SIGNAL("notfound"))
            self.notfound.emit()
====================================
self.found.emit(match.start())是将正则表达式中获得的参数match.start()发射出去,self.notfound.emit()则发射一个空的参数。这两个信号,要发射到__main__函数的form对象中,原来的代码是:
==========================================

form = FindAndReplaceDlg(text)form.connect(form, SIGNAL("found"), found)
form.connect(form, SIGNAL("notfound"), nomore)
===========================================
仍然要改成QT5的发射和接收方式:

form.connect(form, SIGNAL("found"), found)
form.connect(form, SIGNAL("notfound"), nomore)

离线wps2000

只看该作者 4楼 发表于: 2017-06-22
最后,findandreplacedlg.py改后的完整代码为:

=================================================================================

import re
from PyQt5.QtCore import pyqtSlot,pyqtSignal,Qt
from PyQt5.QtWidgets import QDialog,QApplication
import ui_findandreplacedlg

'''
MAC = True
try:
    from PyQt4.QtGui import qt_mac_set_native_menubar
except ImportError:
    MAC = False
'''
MAC = "qt_mac_set_native_menubar" in dir()


class FindAndReplaceDlg(QDialog,
        ui_findandreplacedlg.Ui_FindAndReplaceDlg):


    notfound = pyqtSignal()
    found = pyqtSignal(int)

    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")
    @pyqtSlot(str)
    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("")
    @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(SIGNAL("found"), match.start())
            self.found.emit(match.start())
        else:
            #self.emit(SIGNAL("notfound"))
            self.notfound.emit()


    #@pyqtSignature("")
    @pyqtSlot()
    def on_replaceButton_clicked(self):
        regex = self.makeRegex()
        self.__text = regex.sub(self.replaceLineEdit.text(),
                                self.__text, 1)


    #@pyqtSignature("")
    @pyqtSlot()
    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.found.connect(found)
    form.notfound.connect(nomore)

    form.show()
    app.exec_()
    print(form.text())

离线wps2000

只看该作者 5楼 发表于: 2017-06-22
需要注意的是,在界面文件ui_findandreplacedlg.py中,这行代码:


QtCore.QMetaObject.connectSlotsByName(FindAndReplaceDlg)


这个静态方法,会在窗体窗口部件的各个信号和使用了特定命名规范的子类方法之间创建信号-槽连接。窗体中任何on_widgetName_signalName形式的方法名,都会用窗口部件的信号与之相连接。


比如在findandreplacedlg.py文件中,定义了on_findLineEdit_textEdited方法:

@pyqtSlot(str)
    def on_findLineEdit_textEdited(self, text):
        self.__index = 0
        self.updateUi()

有了对槽@pyqtSlot(str)修示符的定义之后,on_findLineEdit_textEdited就可以与ui_findandreplacedlg.py里面的QtCore.QMetaObject.connectSlotsByName连接。
离线wps2000

只看该作者 6楼 发表于: 2017-06-22
两个文件findandreplacedlg.py和ui_findandreplacedlg.py的信号-槽的关系如下图:



离线wps2000

只看该作者 7楼 发表于: 2017-06-22
而在findandreplacedlg.py中,两个信号found和notfound,信号与槽的关系如下图:

离线mali78120

只看该作者 8楼 发表于: 11-08
刚看到这里,很有启发。谢谢。
快速回复
限100 字节
 
上一个 下一个