0

According to the API the PyQt5 or PySide cell oriented signals of a QTableWidget are supposed to receive two interger parameters for row, and column respectively. For example:

def cellClicked (row, column)

Now when I try to call them like that:

table=QTableWidget(5,5)

def slotCellClick1():
    print('something')

table.cellClicked(0,0).connect(slotCellClick1)

, I get, TypeError: native Qt signal is not callable.

The compiling solution and so far described in examples is in this manner:

table.cellClicked.connect(slotCellClick1)

which works for cell click, in general.

Am I getting wrong the concept or there is still a way to address a specific cell signals with this api functions? Otherwise what would be the workaround to trigger specific cell click signals?

Marjan100
  • 346
  • 1
  • 2
  • 16

2 Answers2

1

That's not how signals and slots work.

Toolkits, and APIs in general, use callbacks to notify the programmer when something happens, by calling a function to react to it; this approach usually provides an interface that can pass some arguments along with the notification.

Suppose you have a module that a certain point can change "something" in it, you want to be notified whenever that change happens and eventually do something with it:

# pseudo code
from some_api import some_object

def some_function(argument):
    print("Something changed to {}!".format(argument))

some_object.set_something_changed_callback(some_function)

>>> some_object.change_something(True)
Something changed to True!

As you can see, the something_changed_callback is not about the possible value of "something", as the callback will be called anyway; if you want to react to a specific value of "something", you'll have to check that within the callback function.

While for simpler apis it's usually fine to have a set_*_changed_callback() for each possible case, in complex toolkits like Qt that would be unnecessary (adding thousands of functions, one for each signal) and confusing.
Qt (like other toolkits like Gtk) uses a similar callback technique but with an unified interface to connect all signals to their "callbacks"; the concept doesn't change that much, at least from the coding perspective, but it makes things easier.
Originally, the syntax was like this:

QObject.connect(some_object, SIGNAL("something_changed(bool)", some_function)

but since some years it's been simplified to the "new style" connection:

some_object.something_changed.connect(some_function)

which is almost the same as the above:

some_object.set_something_changed_callback(some_function)

So, long story short, you can't connect to a specific signal "result", you'll have to check it by yourself.

I can understand your point of view: «I'm interested in calling my slot only when the value is x/y/z». It would make sense, but that kind of interface could be problematic from the api implementation point of view.
Most importantly, a lot of signals emit objects that are class instancies (QModelIndex, QStandardItem, etc) that are created at runtime or even have parents that don't exist yet when you have to connect them, or are mutable objects (one might want to check if a list or dictionary is equal to the one emitted, or if is the same).
Also, some signals have multiple arguments, and one could be interested in checking only some or one of them, but that kind of checking would be almost impossible to create with a simple function argument without any possibility of error or exception. Let's say you want to connect to cellClicked whenever the column is 1, no matter what row; you'd probably think that a good way would be to use cellClicked(None, 1), cellClicked(False, 1) or cellClicked(-1, 1), but some signals actually return None, False or -1, so there wouldn't be a simple standardized way to tell "ignore that argument" (if not by using a custom type).

musicamante
  • 41,230
  • 6
  • 33
  • 58
  • Txn. for the explaination. I whished at least something of this stayed in the documentation, which is misleading. – Marjan100 Nov 27 '19 at 15:28
  • It actually is on the [PyQt documentation](https://www.riverbankcomputing.com/static/Docs/PyQt5/signals_slots.html), but you'll need to read it carefully and possibly integrate it with the more comprehensive explanation in the [Qt docs](https://doc.qt.io/qt-5/signalsandslots.html). – musicamante Nov 27 '19 at 19:13
0

After searching I found an answer that answers my question for a specific case of cellDoubleClicked https://stackoverflow.com/a/46738897/3597222

Marjan100
  • 346
  • 1
  • 2
  • 16