Solved When Use @Slot(), sender has NoneType.
-
Recently, I find when I use @Slot() before a function, the sender() in function will return NoneType. And if I don't use it, everything is OK, is this a bug?
@Slot() def select_leg_button_clicked(self): self.repaint() button = self.sender()
Just like this, the button will be NoneType.
But if I don't use slot, just like:#@Slot() def select_leg_button_clicked(self): self.repaint() button = self.sender()
sender() works well. Is this a bug? Because the example for Pyside2 says we should use @Slot() to reduce some error.
-
Okay I work with PyQt5 but it is a lot like PySide2 that being said and not seeing the rest of your code I would say no its not a bug in the code but a bug in the documentation. Kind of like where the official documentation says you ought to sub-class a QThread when in actuality that is absolutely the wrong way to do it.
Next it appears that this is perhaps the function that a QPushButton calls if so then you definitely do not need the @Slot() decorator because the @Slot() decorator means its a catcher for a Signal which I do not see defined nor implemented here. So I am going to guess you are not using Signals/Slots in which case you have no need for the @Slot() decorator.
-
I use connect to connect the signal and slot,.In my code, it is a radiobutton, here is my code:
self.left_button = QRadioButton('l') self.left_button.clicked.connect(self.select_leg_button_clicked)
This is also the recommended way to connect signal and slot, I think I'm using Signals/Slots. Is there any wrong?
-
Okay to use a @Slot() you need to use a Signal. The connect you are using is not an in code Signal as you have not defined it as such that is just a standard connect that connects your function to a system Signal which seems to be handled differently. Yeah sort of the same thing but not the same thing. This is a basic in code Signal/Slot definition where the MySignal is defined within the Class and used where the Class is instantiated and that is also where the Slot would be defined
MySignal = Signal() self.Class.MySignal.connect(self.select_leg) @Slot() def select_leg(self): pass
This is how you would declare a standard Button and I assume a Radio Button is pretty much the same
self.btnLeft = QPushButton('Left') self.btnLeft.clicked.connect(self.select_leg) def select_leg(): pass
Again while similar they are not the same. I have never had to add a Slot decorator to a function associated with a connect to a predefined object that uses event signals in fact doing so has always caused an error but I have always had to use the Slot decorator when it is associated with a custom in my code defined signal. You can either accept that or continue to struggle with trying to add that @Slot decorator to your function. Of course I am no Guru when it comes to PyQt by any measure (and have never used PySide2) but so far I have seen nothing that contradicts what I have just shown you. If you do find something let me know and I will take a look. Just keep in mind their documentation is not always spot on.