Skip to content

SidebarWidget

Qt Base Class: QMainWindow

Signature: QMainWindow(self, parent: Optional[PySide6.QtWidgets.QWidget] = None, flags: PySide6.QtCore.Qt.WindowType = Default(Qt.WindowFlags)) -> None

Base classes

Name Children Inherits
MainWindow
prettyqt.widgets.mainwindow

⋔ Inheritance diagram

graph TD
  1473367088208["custom_widgets.SidebarWidget"]
  1473296227584["widgets.MainWindow"]
  1473293688240["widgets.WidgetMixin"]
  1473299815024["core.ObjectMixin"]
  140713234304496["builtins.object"]
  1473245548480["gui.PaintDeviceMixin"]
  1473290635936["QtWidgets.QMainWindow"]
  1473290849680["QtWidgets.QWidget"]
  1473288842240["QtCore.QObject"]
  1473291690208["Shiboken.Object"]
  1473300082368["QtGui.QPaintDevice"]
  1473296227584 --> 1473367088208
  1473293688240 --> 1473296227584
  1473299815024 --> 1473293688240
  140713234304496 --> 1473299815024
  1473245548480 --> 1473293688240
  140713234304496 --> 1473245548480
  1473290635936 --> 1473296227584
  1473290849680 --> 1473290635936
  1473288842240 --> 1473290849680
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208
  1473300082368 --> 1473290849680
  1473291690208 --> 1473300082368

🛈 DocStrings

Bases: MainWindow

Source code in prettyqt\custom_widgets\sidebarwidget.py
class SidebarWidget(widgets.MainWindow):
    SETTINGS_BUTTON_HEIGHT = 28

    def __init__(
        self,
        *args,
        show_settings: bool = False,
        main_layout: widgets.widget.LayoutStr | widgets.QLayout = "vertical",
        **kwargs,
    ):
        super().__init__(*args, **kwargs)
        self._button_width = 100
        self._style: constants.ToolButtonStyleStr = "text_below_icon"
        self.button_map: dict[widgets.QWidget, widgets.QToolButton] = {}
        self.icon_map: dict[widgets.QWidget, gui.Icon] = {}
        self.sidebar = widgets.ToolBar(
            context_menu_policy="prevent",
            floatable=True,
            object_name="SidebarWidget",
            window_title="Sidebar",
        )
        self.sidebar.set_style(self._style)
        self.sidebar.set_allowed_areas("all")
        self.settings_menu = widgets.Menu()
        self.sidebar.set_icon_size(int(self._button_width * 0.7))
        if show_settings:
            self.settings_btn = self.sidebar.add_menu_button(
                "", icon="mdi.wrench", menu=self.settings_menu
            )
            self.settings_btn.setFixedSize(
                self._button_width, self.SETTINGS_BUTTON_HEIGHT
            )
            self.settings_btn.set_style("icon")
            self.sidebar.orientationChanged.connect(self._on_orientation_change)
            self.sidebar.add_separator()
        self.spacer_action = self.sidebar.add_spacer()
        self.add_toolbar(self.sidebar, "left")
        self.area = widgets.Widget()
        self.area.set_layout("stacked")
        w = widgets.Widget()
        w.set_layout(main_layout)
        self.main_layout = w.box
        self.main_layout.set_margin(0)
        self.main_layout += self.area
        self.setCentralWidget(w)

    def _on_orientation_change(self, orientation: constants.Orientation):
        if orientation == constants.HORIZONTAL:
            self.settings_btn.setFixedSize(34, 34)
        else:
            self.settings_btn.setFixedSize(
                self._button_width, self.SETTINGS_BUTTON_HEIGHT
            )

    def add_tab(
        self,
        item: widgets.QWidget,
        title: str,
        icon: datatypes.IconType | None = None,
        show: bool = False,
        shortcut: str | None = None,
        area: AreaStr = "top",
    ):
        self.area.box.add(item)
        act = gui.Action(
            text=title,
            icon=icon,
            shortcut=shortcut or "",
            parent=self.sidebar,
            checkable=True,
            triggered=lambda: self.set_tab(item),
        )
        self.addAction(act)
        button = widgets.ToolButton(self.sidebar)
        button.setDefaultAction(act)
        button.setFixedWidth(self._button_width)
        button.set_style(self._style)
        if area == "top":
            self.sidebar.insertWidget(self.spacer_action, button)
        else:
            self.sidebar.addWidget(button)
        if len(self.area.box) == 1:
            button.setChecked(True)
        self.button_map[item] = button
        self.icon_map[item] = iconprovider.get_icon(icon)
        if show:
            self.area.box.setCurrentWidget(item)

    def set_marker(
        self, item: str | int | widgets.Widget, color: datatypes.ColorType = "red"
    ):
        widget = item if isinstance(item, widgets.QWidget) else self._get_widget(item)
        if widget == self._get_current_widget():
            return
        template = self.icon_map[widget]
        px = template.pixmap(100, 100)
        with gui.Painter(px) as painter:
            dot = gui.Pixmap.create_dot(color)
            painter.drawPixmap(0, 0, dot)
        icon = gui.Icon(px)
        self.button_map[widget].setIcon(icon)

    def _get_widget(self, item: str | int | widgets.Widget):
        """Returns widget page specified by name, offset or content."""
        match item:
            case int():
                return self.area.box[item]
            case str():
                w = self.area.find_child(widgets.QWidget, name=item, recursive=False)
                if w not in self.area.box:
                    raise ValueError("Layout does not contain the chosen widget")
                return w
            case _:
                raise TypeError(item)

    def _get_current_widget(self) -> widgets.QWidget:
        """Returns the currently selected widget page."""
        for k, v in self.button_map.items():
            if v.isChecked():
                return k
        raise RuntimeError("no page activated.")

    def set_tab(self, item: str | int | widgets.QWidget):
        widget = item if isinstance(item, widgets.QWidget) else self._get_widget(item)
        current = self.area.box.currentWidget()
        self.button_map[current].setChecked(False)
        self.area.box.setCurrentWidget(widget)
        self.button_map[widget].setChecked(True)

    def add_spacer(self) -> gui.QAction:
        return self.sidebar.add_spacer()

    def add_separator(self, text: str | None = None, area: AreaStr = "top"):
        if area == "top":
            self.sidebar.add_separator(text, before=self.spacer_action)
        else:
            self.sidebar.add_separator(text)

    def add_action(self, *args, area: AreaStr = "top", **kwargs):
        act = args[0] if args else gui.Action(**kwargs)
        self.addAction(act)
        button = widgets.ToolButton(self.sidebar)
        button.setDefaultAction(act)
        button.setFixedWidth(self._button_width)
        button.set_style(self._style)
        if area == "top":
            self.sidebar.insertWidget(self.spacer_action, button)
        else:
            self.sidebar.addWidget(button)
        return act

⌗ Property table

Qt Property Type Doc
objectName QString
modal bool
windowModality Qt::WindowModality
enabled bool
geometry QRect
frameGeometry QRect
normalGeometry QRect
x int
y int
pos QPoint
frameSize QSize
size QSize
width int
height int
rect QRect
childrenRect QRect
childrenRegion QRegion
sizePolicy QSizePolicy
minimumSize QSize
maximumSize QSize
minimumWidth int
minimumHeight int
maximumWidth int
maximumHeight int
sizeIncrement QSize
baseSize QSize
palette QPalette
font QFont
cursor QCursor
mouseTracking bool
tabletTracking bool
isActiveWindow bool
focusPolicy Qt::FocusPolicy
focus bool
contextMenuPolicy Qt::ContextMenuPolicy
updatesEnabled bool
visible bool
minimized bool
maximized bool
fullScreen bool
sizeHint QSize
minimumSizeHint QSize
acceptDrops bool
windowTitle QString
windowIcon QIcon
windowIconText QString
windowOpacity double
windowModified bool
toolTip QString
toolTipDuration int
statusTip QString
whatsThis QString
accessibleName QString
accessibleDescription QString
layoutDirection Qt::LayoutDirection
autoFillBackground bool
styleSheet QString
locale QLocale
windowFilePath QString
inputMethodHints QFlags
iconSize QSize
toolButtonStyle Qt::ToolButtonStyle
animated bool
documentMode bool
tabShape QTabWidget::TabShape
dockNestingEnabled bool
dockOptions QFlags
unifiedTitleAndToolBarOnMac bool