Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions collagraph/renderers/pyside/objects/qobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,12 @@ def set_attribute(self, attr, value):
setattr(self, attr, value)
return

block_signals = False
if hasattr(self, "blockSignals"):
self.blockSignals(True)
block_signals = True

call_method(method, value)

if block_signals:
Comment thread
Korijn marked this conversation as resolved.
self.blockSignals(False)
71 changes: 70 additions & 1 deletion tests/pyside/test_pyside_trigger_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

pytest.importorskip("PySide6")

from PySide6 import QtWidgets
from PySide6 import QtGui, QtWidgets

import collagraph as cg

Expand Down Expand Up @@ -203,3 +203,72 @@ def item_in_list(index: int, value: str):
item_index = model.index(0, 0)
model.setItemData(item_index, {0: "e"})
assert Tree.changed == 1


ACTION = """
<window>
<menubar>
<menu title="Menu">
<action
:text="text"
object_name="test_action"
@changed="on_changed"
/>
</menu>
</menubar>
<widget />
</window>

<script>
import collagraph as cg

class Action(cg.Component):
changed = 0

def on_changed(self):
Action.changed += 1
</script>
"""


def test_qaction_change_no_trigger(qapp, qtbot, parse_source):
"""
When a QAction's attribute is updated by the renderer,
it should not trigger the `changed` signal.
"""
Action, _ = parse_source(ACTION)
state = reactive({"text": "initial"})

renderer = cg.PySideRenderer(autoshow=False)
gui = cg.Collagraph(renderer=renderer)
gui.render(Action, qapp, state=state)
assert Action.changed == 0

def action_found(text):
windows = [
widget
for widget in qapp.topLevelWidgets()
if isinstance(widget, QtWidgets.QMainWindow)
]
assert len(windows) == 1
action = windows[0].findChild(QtGui.QAction, "test_action")
assert action and action.text() == text

qtbot.waitUntil(partial(action_found, "initial"), timeout=500)
assert Action.changed == 0

# Change the text via reactive state - this should NOT trigger the signal
state["text"] = "updated"

qtbot.waitUntil(partial(action_found, "updated"), timeout=500)
assert Action.changed == 0

# Directly change the action's text - this SHOULD trigger the signal
windows = [
widget
for widget in qapp.topLevelWidgets()
if isinstance(widget, QtWidgets.QMainWindow)
]
action = windows[0].findChild(QtGui.QAction, "test_action")
action.setText("manual")
assert Action.changed == 1