Skip to content
Open
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
43 changes: 37 additions & 6 deletions src/superqt/collapsible/_collapsible.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,41 @@
Qt,
Signal,
)
from qtpy.QtGui import QIcon, QPainter, QPalette, QPixmap
from qtpy.QtWidgets import QFrame, QPushButton, QSizePolicy, QVBoxLayout, QWidget
from qtpy.QtGui import QIcon, QPainter, QPaintEvent, QPalette, QPixmap
from qtpy.QtWidgets import (
QApplication,
QFrame,
QSizePolicy,
QStyle,
QStyleOptionToolButton,
QStylePainter,
QToolButton,
QVBoxLayout,
QWidget,
)


class _GhostToolButton(QToolButton):
"""Tool button that keeps a ghost appearance while checked."""

def __init__(self, parent: QWidget | None = None, *, title: str = "") -> None:
super().__init__(parent)
if title:
self.setText(title)
# Match QPushButton typography from main for consistent header text size.
self.setFont(QApplication.font("QPushButton"))
self.setCheckable(True)
self.setAutoRaise(True)
self.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextBesideIcon)

def paintEvent(self, a0: QPaintEvent | None) -> None:
if not a0:
return super().paintEvent(a0)
option = QStyleOptionToolButton()
self.initStyleOption(option)
option.state &= ~QStyle.StateFlag.State_On
painter = QStylePainter(self)
painter.drawControl(QStyle.ControlElement.CE_ToolButtonLabel, option)
Comment on lines +46 to +50
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how to fix this, but this branch prevents from apply any style using stylesheet



class QCollapsible(QFrame):
Expand All @@ -39,13 +72,11 @@ def __init__(
self._is_animating = False
self._text = title

self._toggle_btn = QPushButton(title)
self._toggle_btn.setCheckable(True)
self._toggle_btn = _GhostToolButton(self, title=title)
self.setCollapsedIcon(icon=collapsedIcon)
self.setExpandedIcon(icon=expandedIcon)
self.setSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Maximum)

self._toggle_btn.setStyleSheet("text-align: left; border: none; outline: none;")
self._toggle_btn.toggled.connect(self._toggle)

# frame layout
Expand All @@ -68,7 +99,7 @@ def __init__(
_content.layout().setContentsMargins(QMargins(5, 0, 0, 0))
self.setContent(_content)

def toggleButton(self) -> QPushButton:
def toggleButton(self) -> QToolButton:
"""Return the toggle button."""
return self._toggle_btn

Expand Down
Loading