[PySide] What if a slot is not decorated with @Slot()?
Zhao Lee
redstone-cold at 163.com
Sat Nov 19 03:47:38 CET 2022
Thanks Chris!
The question originated from the issue :
In my project, self.sender() in the downloadFinished slot sometimes returns None instead of QNetworkReply, if comment the @Slot() ahead it seems to always return QNetworkReply - works well. The same issue also happened to the slot connected to the finished signal of QProcess. But the issue didn't happen for the PyQt5 version in my project. I reduced my senario to the following lite demo, but haven't seen the issue happened again, very strange !
Test environment : Python 3.9.13 on win11 64bit, latest PySide6.
from PySide6.QtCore import*
from PySide6.QtCore import Slot
from PySide6.QtCore import Signal
from PySide6.QtGui import*
from PySide6.QtWidgets import*
from PySide6.QtNetwork import*
import sys
classExample(QMainWindow):
def__init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.manager =QNetworkAccessManager(self)
self.reply2DownloadFileTuple={}
self.doRequest()
defdoRequest(self):
url ='https://download-cdn.jetbrains.com/python/pycharm-professional-2022.2.3.exe'
request =QNetworkRequest(QUrl(url))
reply =self.manager.get(request)
self.reply2DownloadFileTuple[reply] =None
reply.finished.connect(self.downloadFinished)
reply.downloadProgress.connect(self.downloadProgress)#, Qt.QueuedConnection
@Slot(int,int)
defdownloadProgress(self, bytesReceived, bytesTotal):
reply =self.sender()
print(bytesReceived/bytesTotal,reply)
@Slot()
defdownloadFinished(self):
reply =self.sender()
print('reply-----------',reply)
ifnotisinstance(reply, QNetworkReply):
print('not QNetworkReply')
return
# er = reply.error()
# if er == QNetworkReply.NetworkError.NoError:
# bytes_string = reply.readAll()
# print(str(bytes_string, 'utf-8'))
# else:
# print("Error occured: ", er)
# print(reply.errorString())
# QCoreApplication.quit()
defmain():
app=QApplication(sys.argv)
win =Example()
win.show()
sys.exit(app.exec())
if __name__ =='__main__':
main()
At 2022-11-18 19:02:29, "Christian Tismer-Sperling" <tismer at stackless.com> wrote:
>Hi Zhao,
>
>there is a configuration problem between Qt and PySide signals and
>slots. The Qt signals and slots are assigned once at compile time.
>
>On PySide, initialization is different since some things happen
>earlier or later. There *can* be situations constructed which go wrong,
>although doing that is difficult. You will not want to run into these
>problems by accident.
>
>Please believe me as one of the implementers:
>There is only one safe way to create reliable signals and slots.
>Use the decorator!
>
>Cheers - Chris
>
>
>On 16.11.22 15:18, Zhao Lee wrote:
>> I initially expect that the aim of the decorator @Slot() is to make
>> things easier like this
>> https://uwsgi-docs.readthedocs.io/en/latest/PythonDecorators.html#notes
>> <https://uwsgi-docs.readthedocs.io/en/latest/PythonDecorators.html#notes>
>> and without the decorator , you'd expect write a little more code like
>> https://uwsgi-docs.readthedocs.io/en/latest/Signals.html?#timers
>> <https://uwsgi-docs.readthedocs.io/en/latest/Signals.html?#timers>
>> But decorators in PySide become a little different. I know the decorator
>> can make the signal and slot connection a bit easier in PyQt in this way
>> */ @Slot(QModelIndex)/*
>> */ def on_downloadedView_doubleClicked(self, index) :/*
>> */ pass/*
>> But I still doubt the necessity of the decorator here if we write the
>> slot method name in this way. I wonder if it is possible to simplify the
>> mechanism in PySide like that in the above example of uwsgi without
>> requiring it is mandatory(In fact as I posted in last email, it seems
>> still ok without the decorator, although the doc states it is mandatory
>> to exist).
>
>
>--
>Christian Tismer-Sperling :^) tismer at stackless.com
>Software Consulting : http://www.stackless.com/
>Strandstraße 37 : https://github.com/PySide
>24217 Schönberg : GPG key -> 0xFB7BEE0E
>phone +49 173 24 18 776 fax +49 (30) 700143-0023
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20221119/022a23cd/attachment-0001.htm>
More information about the PySide
mailing list