mirror of
https://github.com/pyapp-kit/superqt.git
synced 2025-12-15 18:50:05 +01:00
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:
@@ -1,19 +1,31 @@
|
||||
"""A collapsible widget to hide and unhide child widgets"""
|
||||
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
|
||||
|
||||
|
||||
class QCollapsible(QFrame):
|
||||
"""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
|
||||
"""
|
||||
|
||||
_EXPANDED = "▼ "
|
||||
_COLLAPSED = "▲ "
|
||||
|
||||
toggled = Signal(bool)
|
||||
|
||||
def __init__(self, title: str = "", parent: Optional[QWidget] = None):
|
||||
super().__init__(parent)
|
||||
self._locked = False
|
||||
@@ -123,6 +135,8 @@ class QCollapsible(QFrame):
|
||||
else:
|
||||
self._content.setMaximumHeight(_content_height if forward else 0)
|
||||
|
||||
self.toggled.emit(direction == QPropertyAnimation.Direction.Forward)
|
||||
|
||||
def _toggle(self):
|
||||
self.expand() if self.isExpanded() else self.collapse()
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""A test module for testing collapsible"""
|
||||
|
||||
from qtpy.QtCore import QEasingCurve
|
||||
from qtpy.QtCore import QEasingCurve, Qt
|
||||
from qtpy.QtWidgets import QPushButton
|
||||
|
||||
from superqt import QCollapsible
|
||||
@@ -85,3 +85,16 @@ def test_changing_text(qtbot):
|
||||
wdg.setText("Hi new text")
|
||||
assert wdg.text() == "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()
|
||||
|
||||
Reference in New Issue
Block a user