# collapsible_announcement.py from PySide6.QtWidgets import QWidget, QVBoxLayout, QToolButton, QTextEdit, QScrollArea from PySide6.QtCore import Qt class CollapsibleAnnouncement(QWidget): def __init__(self, position, title, content, parent=None): super().__init__(parent) self.init_ui(position, title, content) def init_ui(self, position, title, content): self.main_layout = QVBoxLayout(self) self.main_layout.setSpacing(5) self.main_layout.setContentsMargins(0, 0, 0, 0) # 타이틀 버튼: 클릭하면 상세 내용이 확장/축소됨 self.toggle_button = QToolButton(text=f"{position}. {title}") self.toggle_button.setCheckable(True) self.toggle_button.setChecked(False) self.toggle_button.setFixedHeight(30) # 모던한 디자인: 폰트 크기, 굵기, 여백 등을 조정 self.toggle_button.setStyleSheet(""" QToolButton { font-size: 16px; font-weight: bold; background-color: #1877f2; color: white; border: none; border-radius: 4px; padding: 5px; } QToolButton:checked { background-color: #166fe5; } """) self.toggle_button.clicked.connect(self.on_toggle) self.main_layout.addWidget(self.toggle_button) # 상세 내용 영역: 처음에는 숨김 self.content_area = QTextEdit() self.content_area.setReadOnly(True) self.content_area.setHtml(content) self.content_area.setFixedHeight(220) self.content_area.setFixedWidth(600) self.content_area.setVisible(False) self.main_layout.addWidget(self.content_area) self.setLayout(self.main_layout) def on_toggle(self): # 토글 버튼의 체크 상태에 따라 상세 내용 영역을 보여주거나 숨깁니다. self.content_area.setVisible(self.toggle_button.isChecked()) # 클릭 시 포함된 스크롤 영역에서 이 위젯을 보이도록 합니다. scroll_area = self.find_scroll_area() if scroll_area: scroll_area.ensureWidgetVisible(self) def find_scroll_area(self): """자신을 포함하는 QScrollArea를 찾아 반환합니다.""" parent = self.parentWidget() while parent: if parent.__class__.__name__ == "QScrollArea": return parent parent = parent.parentWidget() return None