alphanumeric
alphanumeric

Reputation: 19329

PyQt: Single Signal For All

A single QTreeWidget:

tree=QtGui.QTreeWidget()

A single QTreeWidgetItem:

item=QtGui.QTreeWidgetItem()

Adding an item to tree:

tree.addTopLevelItem(item)

One-by-one adding 4 different widgets to item:

tree.setItemWidget(item, 1, QtGui.QLineEdit() )
tree.setItemWidget(item, 2, QtGui.QComboBox() )
tree.setItemWidget(item, 3, QtGui.QDateEdit() )
tree.setItemWidget(item, 4, QtGui.QCheckBox() )

Now in one singe loop I need to connect all 4 sub-item widgets to the same function. Non-working example:

for i in range(1,5):
    tree.itemWidget(item, i).activated.connect(myFunction)

The problem with example above: not all 4 widgets have the same .activated Signal.

QLineEdit() for example has its own .textChanged, QDateEdit() comes with .dateChanged and QCheckBox() with .stateChanged.

What I am looking for is a single Signal solution that could be used with all sub-widgets (particularly interested in one that gets triggered on click (first or just any click).

Upvotes: 0

Views: 111

Answers (1)

Oliver
Oliver

Reputation: 29463

You can't. But signals can be forwarded so you could derive from QLineEdit and create a custom signal "activated" that connects to textChanged signal in QLineEdit init. This is what I would call an "adapter" signal, you would need to derive each one of your classes. But this is extra work that is only worth it if it saves you work in other ways. Otherwise, simpler to have an if/else sequence based on type of widget, but it is somewhat frowned upon to use isclass. So if extra work not worth effort, and don't want to use type-based if/else to connect to proper signal, your only option is to connect to the appropriate signal after calling tree.setItemWidget(item, index, widget).

Upvotes: 1

Related Questions