-3

I see the code below from zetcode:

redb.clicked[bool].connect(self.setColor)

I think the clicked[bool] is not legal in Python. Although I know it may used to show the arguments, it troubles me how to use it.

I just see the Qt doc qt.io, and only know that clicked() has a 'bool' parameter.

northland
  • 27
  • 4
  • It means accessing some value from the object, e.g. by [index](https://docs.python.org/3/tutorial/introduction.html#lists) or [key](https://docs.python.org/3/tutorial/datastructures.html#dictionaries). What it actually _does_ is up to the implementation of [`__getitem__`](https://docs.python.org/3/reference/datamodel.html#object.__getitem__) on the object in question. – jonrsharpe May 14 '23 at 08:28
  • As to that specific usage, if you read the tutorial you linked it tells you exactly what it does: _"We connect a clicked signal to our user defined method. We use the clicked signal that operates with a Boolean value."_ – jonrsharpe May 14 '23 at 08:31
  • Start by reading [the documentation](https://www.riverbankcomputing.com/static/Docs/PyQt5/signals_slots.html) and also [this answer](https://stackoverflow.com/a/53110495) which explains how signal overloads are implemented in python (and why/how they should be used in certain cases). – musicamante May 14 '23 at 10:48

1 Answers1

-2

This is a specific syntax of PyQt, but not a part of the standard Python language.

I mean, the PyQt5 has overloaded the square brackets '[]', to create a concise way to connect the signal and the slot. [bool] hints a method/signal clicked 's "overload", which has a bool type argument to the setColor, though there's always no necessary, maybe no use, to do that.

So it's OK to code:

redb.clicked.connect(self.setColor)

without any hint.

northland
  • 27
  • 4
  • 1
    I've been doing pyqt for a couple of years now, and I've never seen this before. I've also never encountered a signal that has multiple signatures where you'd have to use the brackets to select the desired overload. But it sort of functions like a type hint so it can be useful for readability. – mahkitah May 14 '23 at 09:55
  • 1
    Sorry but almost all you wrote is invalid or imprecise. "Not a part of the standard Python" makes no sense, as it **is** part Python to allow overriding of magic methods such as `__getitem__`; the fact that it's an advanced feature rarely implemented doesn't make it "non standard". That syntax is also not just a hint, but a *specification* of the overload to ensure that the slot will receive the correct signature. And "always no necessary, maybe no use" is completely wrong, as in certain cases it could even be mandatory, if the signal has multiple overloads and actual slots must be connected. – musicamante May 14 '23 at 10:53