class CheckBoxHeader(QHeaderView): checkBoxClicked = Signal(bool) def init( self,…
- User
class CheckBoxHeader(QHeaderView): checkBoxClicked = Signal(bool)
def __init__( self, orientation: Qt.Orientation = Qt.Orientation.Horizontal, parent=None, ): super().__init__(orientation, parent) self.setSectionsClickable(True) self._isChecked = False self.setHighlightSections(False) def _get_style(self) -> QStyle: style = self.style() print(f"Style: {style}") return style if style is not None else QApplication.style() def paintSection(self, painter, rect: QRect, logicalIndex: int) -> None: if painter is None: print("Painter is None") return painter.setPen(Qt.GlobalColor.red) painter.drawRect(rect) print(f"Section {logicalIndex} rect: {rect}") # Отладка super().paintSection(painter, rect, logicalIndex) if logicalIndex == 0: option = QStyleOptionButton() style = self._get_style() # Получаем стандартный размер чекбокса default_check_box_size = ( style.pixelMetric(QStyle.PixelMetric.PM_IndicatorWidth), style.pixelMetric(QStyle.PixelMetric.PM_IndicatorHeight), ) check_box_width, check_box_height = default_check_box_size print(f"Default CheckBox size: {check_box_width}x{check_box_height}") # Убедимся, что чекбокс не больше секции заголовка check_box_width = min(check_box_width, rect.width()) check_box_height = min(check_box_height, rect.height()) x = rect.x() + (rect.width() - check_box_width) // 2 y = rect.y() + (rect.height() - check_box_height) // 2 option.rect = QRect(x, y, check_box_width, check_box_height) print(f"Final CheckBox rect: {option.rect}") # Проверяем координаты option.state = QStyle.StateFlag.State_Enabled | ( QStyle.StateFlag.State_On if self._isChecked else QStyle.StateFlag.State_Off ) painter.setPen(Qt.GlobalColor.green) painter.drawRect(option.rect) style.drawControl(QStyle.ControlElement.CE_CheckBox, option, painter, self) def mousePressEvent(self, e: QMouseEvent) -> None: pos = e.position().toPoint() if hasattr(e, "position") else e.pos() logicalIndex = self.logicalIndexAt(pos) if logicalIndex == 0: # Вычисляем прямоугольник первой секции x = self.sectionPosition(0) width = self.sectionSize(0) height = self.height() rect = QRect(x, 0, width, height) option = QStyleOptionButton() style = self._get_style() check_box_size = style.sizeFromContents( QStyle.ContentsType.CT_CheckBox, option, rect.size(), self ) # Вычисляем положение чекбокса в пределах секции cb_x = rect.x() + (rect.width() - check_box_size.width()) // 2 cb_y = rect.y() + (rect.height() - check_box_size.height()) // 2 checkBoxRect = QRect( cb_x, cb_y, check_box_size.width(), check_box_size.height() ) if checkBoxRect.contains(pos): self._isChecked = not self._isChecked self.checkBoxClicked.emit(self._isChecked) self.updateSection(0) return super().mousePressEvent(e) - Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.