Python 更改 QLineEdit 文本时执行操作(以编程方式)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22531578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:06:33  来源:igfitidea点击:

Make an action when the QLineEdit text is changed (programmatically)

pythonpyqt

提问by Matteo NNZ

I have written the following code snippet with an QLineEdit that can be edited by pushing the button "Add Text".

我用 QLineEdit 编写了以下代码片段,可以通过按下“添加文本”按钮进行编辑。

import sys
import os
from PyQt4 import QtGui
from PyQt4 import *

class SmallGUI(QtGui.QMainWindow):
    def __init__(self):
        super(SmallGUI,self).__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(300,300,300,300)
        self.setWindowTitle('Sample')

        #One input
        self.MyInput = QtGui.QLineEdit(self)
        self.MyInput.setGeometry(88,25,110,20)
        ###############

        QtCore.QObject.connect(self.MyInput,QtCore.SIGNAL("textChanged(bool)"),self.doSomething)

        #Add Text
        self.MyButton = QtGui.QPushButton(self)
        self.MyButton.setGeometry(QtCore.QRect(88,65,110,20))
        self.MyButton.setText('Add Text')
        ###############

        QtCore.QObject.connect(self.MyButton,QtCore.SIGNAL("clicked(bool)"),self.addText)

        self.show()

    def addText(self):
        self.MyInput.setText('write something')

    def doSomething(self):
        print "I'm doing something"

def main():
    app = QtGui.QApplication(sys.argv)
    sampleForm = SmallGUI()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

What I would like to do is to execute an action when the text of the QLineEdit is changed programmatically, i.e. by clicking the button 'Add Text', doing the following:

我想要做的是在 QLineEdit 的文本以编程方式更改时执行一个操作,即通过单击“添加文本”按钮,执行以下操作:

QtCore.QObject.connect(self.MyInput,QtCore.SIGNAL("textChanged(bool)"),self.doSomething)

The reason why I have used the signal "textChanged" is related to what the class documentationsays, that is "this signal is also emitted when the text is changed programmatically, for example, by calling setText()."

我使用信号“textChanged”的原因与类文档所说的有关,即“当以编程方式更改文本时也会发出此信号,例如,通过调用 setText()”。

However this does not work cause the print statement is not executed. Can anyone help me out with that?

但是,这不起作用,因为不执行打印语句。谁能帮我解决这个问题?

采纳答案by Bakuriu

The problem is that the signal is nottextChanged(bool)because it takes a string argument, so it should probably bee: textChanged(str).

问题是信号不是textChanged(bool)因为它需要一个字符串参数,所以它可能应该是 bee: textChanged(str)

To avoid this kind of errors you should use the new-style syntaxfor connecting signals:

为了避免这种错误,您应该使用新式语法来连接信号:

self.MyInput.textChanged.connect(self.doSomething)
# or:
self.MyInput.textChanged[str].connect(self.doSomething)

This syntax has several advantages:

这种语法有几个优点:

  • It is clearer
  • It's less verbose and more readable
  • It provides more error checking because if the signal doesn't exist it raises an error. With the old syntax no error is raised, but the signal isn't connected either and the result is the behaviour you have seen.
  • 更清晰
  • 它不那么冗长,更具可读性
  • 它提供了更多的错误检查,因为如果信号不存在,它会引发错误。使用旧语法不会引发错误,但信号也未连接,结果是您所看到的行为。