Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

python 3.x - connect function in pyqt5 does not work

I recently moved from pyside to pyqt5 and there is a problem. I looked it up online and apparently, it already happened to people who used pyqt4 and moved to pyqt5. However, it didn't really help... I tried to add pyqtSignal after Qobject but it is still not working. Please help. these are my code lines:

QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"),Dialog.accept)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), Dialog.reject)

and this is what appears when I run it:

AttributeError: type object 'QObject' has no attribute 'connect'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

from the docs:

connect(slot[, type=PyQt5.QtCore.Qt.AutoConnection[, no_receiver_check=False]])

Connect a signal to a slot. An exception will be raised if the connection failed. Parameters:

  • slot – the slot to connect to, either a Python callable or another bound signal.
  • type – the type of the connection to make.
  • no_receiver_check – suppress the check that the underlying C++ receiver instance still exists and deliver the signal anyway.

for your example:

self.buttonBox.accepted.connect(Dialog.accept) # pyqt5

QtCore.QObject.connect(self.buttonBox.rejected, Dialog.reject) # pyqt4

As a sidenote, "Dialog" sounds like a class, you probably want to connect to an instance, else think about naming your instances with lowercase front-letters ...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...