简体   繁体   中英

PySide (Qt) signal not reaching my slot

I have this simplified code that doesn't work and I can't understand why... I expect MySlot.slt() to be called every time i press a key in my QTextEdit but it doesn't! Could you please have a look?

#!/usr/bin/env python2

import sys
from PySide import QtGui, QtCore

class MySlot(object):
    def __init__(self, qte):
        qte.textChanged.connect(self.soc)

    def slt(self):
        print("got signal")

class MainWid(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MainWid, self).__init__(parent)
        self.initgui()

    def initgui(self):
        lay = QtGui.QVBoxLayout()
        txt = QtGui.QTextEdit(self)
        MySoc(txt)

        lay.addWidget(txt)
        self.setLayout(lay)
        self.show()

def main():
    app = QtGui.QApplication(sys.argv)
    wid = MainWid()
    sys.exit(app.exec_())

if __name__=="__main__":
    main()

Your MySoc object in initgui has local scope and is therefore destroyed at the end of initgui .

Assign the object to a variable:

...
self.soc = MySoc(txt);
...

and you will see the "got signal" output each time you press a key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM