Add signal to QCollapsible (#142)

* add signal when toggle button is clicked

* emit signal when expand/collapse are called. emit bool. add to test.

* fix signal emission

Co-authored-by: Talley Lambert <talley.lambert@gmail.com>
This commit is contained in:
Pam
2022-11-27 19:43:05 -06:00
committed by GitHub
parent e98936e8d8
commit 3df7f49706
2 changed files with 29 additions and 2 deletions

View File

@@ -1,19 +1,31 @@
"""A collapsible widget to hide and unhide child widgets""" """A collapsible widget to hide and unhide child widgets"""
from typing import Optional from typing import Optional
from qtpy.QtCore import QEasingCurve, QEvent, QMargins, QObject, QPropertyAnimation, Qt from qtpy.QtCore import (
QEasingCurve,
QEvent,
QMargins,
QObject,
QPropertyAnimation,
Qt,
Signal,
)
from qtpy.QtWidgets import QFrame, QPushButton, QVBoxLayout, QWidget from qtpy.QtWidgets import QFrame, QPushButton, QVBoxLayout, QWidget
class QCollapsible(QFrame): class QCollapsible(QFrame):
"""A collapsible widget to hide and unhide child widgets. """A collapsible widget to hide and unhide child widgets.
A signal is emitted when the widget is expanded (True) or collapsed (False).
Based on https://stackoverflow.com/a/68141638 Based on https://stackoverflow.com/a/68141638
""" """
_EXPANDED = "" _EXPANDED = ""
_COLLAPSED = "" _COLLAPSED = ""
toggled = Signal(bool)
def __init__(self, title: str = "", parent: Optional[QWidget] = None): def __init__(self, title: str = "", parent: Optional[QWidget] = None):
super().__init__(parent) super().__init__(parent)
self._locked = False self._locked = False
@@ -123,6 +135,8 @@ class QCollapsible(QFrame):
else: else:
self._content.setMaximumHeight(_content_height if forward else 0) self._content.setMaximumHeight(_content_height if forward else 0)
self.toggled.emit(direction == QPropertyAnimation.Direction.Forward)
def _toggle(self): def _toggle(self):
self.expand() if self.isExpanded() else self.collapse() self.expand() if self.isExpanded() else self.collapse()

View File

@@ -1,6 +1,6 @@
"""A test module for testing collapsible""" """A test module for testing collapsible"""
from qtpy.QtCore import QEasingCurve from qtpy.QtCore import QEasingCurve, Qt
from qtpy.QtWidgets import QPushButton from qtpy.QtWidgets import QPushButton
from superqt import QCollapsible from superqt import QCollapsible
@@ -85,3 +85,16 @@ def test_changing_text(qtbot):
wdg.setText("Hi new text") wdg.setText("Hi new text")
assert wdg.text() == "Hi new text" assert wdg.text() == "Hi new text"
assert wdg._toggle_btn.text() == QCollapsible._COLLAPSED + "Hi new text" assert wdg._toggle_btn.text() == QCollapsible._COLLAPSED + "Hi new text"
def test_toggle_signal(qtbot):
"""Test that signal is emitted when widget expanded/collapsed."""
wdg = QCollapsible()
with qtbot.waitSignal(wdg.toggled, timeout=500):
qtbot.mouseClick(wdg._toggle_btn, Qt.LeftButton)
with qtbot.waitSignal(wdg.toggled, timeout=500):
wdg.expand()
with qtbot.waitSignal(wdg.toggled, timeout=500):
wdg.collapse()