PyQt4 signals and slots

2019-03-08 17:09发布

I am writing my first Python app with PyQt4. I have a MainWindow and a Dialog class, which is a part of MainWindow class:

self.loginDialog = LoginDialog();

I use slots and signals. Here's a connection made in MainWindow:

QtCore.QObject.connect(self.loginDialog, QtCore.SIGNAL("aa(str)"), self.login)

And I try to emit signal inside the Dialog class (I'm sure it is emitted):

self.emit(QtCore.SIGNAL("aa"), "jacek")

Unfortunately, slot is not invoked. I tried with no arguments as well, different styles of emitting signal. No errors, no warnings in the code. What might be the problem?

7条回答
冷血范
2楼-- · 2019-03-08 17:38

As noted by gruszczy you have to use the same QtCore.SIGNAL('xxx') to connect signal and to emit it. Also I think you should use Qt types in the arguments list of signal function. E.g.:

QtCore.QObject.connect(self.loginDialog, QtCore.SIGNAL("aa(QString&)"), self.login)

And then emit with:

self.emit(QtCore.SIGNAL("aa(QString&)"), "jacek")

Sometimes it makes sense to define signal only once as global variable and use it elsewhere:

MYSIGNAL = QtCore.SIGNAL("aa(QString&)")
...
QtCore.QObject.connect(self.loginDialog, MYSIGNAL, self.login)
...
self.emit(MYSIGNAL, "jacek")
查看更多
登录 后发表回答