Skip to content

AnnotatedScrollBar

Qt Base Class: QScrollBar

Signature: QScrollBar(self, arg__1: PySide6.QtCore.Qt.Orientation, parent: Optional[PySide6.QtWidgets.QWidget] = None) -> None QScrollBar(self, parent: Optional[PySide6.QtWidgets.QWidget] = None) -> None

Base classes

Name Children Inherits
ScrollBar
prettyqt.widgets.scrollbar

⋔ Inheritance diagram

graph TD
  1473367013056["custom_widgets.AnnotatedScrollBar"]
  1473293693120["widgets.ScrollBar"]
  1473293699952["widgets.ScrollBarMixin"]
  1473293701904["widgets.AbstractSliderMixin"]
  1473293688240["widgets.WidgetMixin"]
  1473299815024["core.ObjectMixin"]
  140713234304496["builtins.object"]
  1473245548480["gui.PaintDeviceMixin"]
  1473365414912["QtWidgets.QScrollBar"]
  1473365440288["QtWidgets.QAbstractSlider"]
  1473290849680["QtWidgets.QWidget"]
  1473288842240["QtCore.QObject"]
  1473291690208["Shiboken.Object"]
  1473300082368["QtGui.QPaintDevice"]
  1473293693120 --> 1473367013056
  1473293699952 --> 1473293693120
  1473293701904 --> 1473293699952
  1473293688240 --> 1473293701904
  1473299815024 --> 1473293688240
  140713234304496 --> 1473299815024
  1473245548480 --> 1473293688240
  140713234304496 --> 1473245548480
  1473365414912 --> 1473293693120
  1473365440288 --> 1473365414912
  1473290849680 --> 1473365440288
  1473288842240 --> 1473290849680
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208
  1473300082368 --> 1473290849680
  1473291690208 --> 1473300082368

🛈 DocStrings

Bases: ScrollBar

ScrollBar which can highlight user-defined ranges.

widget = widgets.PlainTextEdit("\n".join(str(i) for i in range(1000)))
widget.v_scrollbar = AnnotatedScrollBar(constants.VERTICAL)
widget.v_scrollbar.set_annotations([(10, 20), (50, 60), (82, 85)])

Image title

Annotated ScrollBar
Source code in prettyqt\custom_widgets\scrollbars\annotatedscrollbar.py
class AnnotatedScrollBar(widgets.ScrollBar):
    r"""ScrollBar which can highlight user-defined ranges.

    ``` py
    widget = widgets.PlainTextEdit("\n".join(str(i) for i in range(1000)))
    widget.v_scrollbar = AnnotatedScrollBar(constants.VERTICAL)
    widget.v_scrollbar.set_annotations([(10, 20), (50, 60), (82, 85)])
    ```

    <figure markdown>
      ![Image title](../../images/annotatedscrollbar.png)
      <figcaption>Annotated ScrollBar</figcaption>
    </figure>
    """

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._annotation_color = gui.QColor("gold")
        self._annotations = []
        self._document_length = 100

    @classmethod
    def create_example(cls):
        widget = widgets.PlainTextEdit("\n".join(str(i) for i in range(1000)))
        widget.v_scrollbar = AnnotatedScrollBar(constants.VERTICAL)
        widget.v_scrollbar.set_annotations([(10, 20), (50, 60), (82, 85)])
        return widget

    def set_annotations(self, annotations: list[tuple[int, int]]):
        self._annotations = annotations

    def get_annotations(self) -> list[tuple[int, int]]:
        return self._annotations

    def add_annotation(self, annotation: tuple[int, int]):
        self._annotations.append(annotation)

    def paintEvent(self, event):
        super().paintEvent(event)
        with gui.Painter(self) as p:
            opt = widgets.QStyleOptionSlider()
            self.initStyleOption(opt)
            gr = self.style().subControlRect(
                CC_ScrollBar, opt, SubControl.SC_ScrollBarGroove, self
            )
            sr = self.style().subControlRect(
                CC_ScrollBar, opt, SubControl.SC_ScrollBarSlider, self
            )
            p.setClipRegion(
                gui.QRegion(gr) - gui.QRegion(sr), constants.ClipOperation.IntersectClip
            )
            x, y, w, h = gr.getRect()
            c = gui.QColor(self._annotation_color)
            p.setBrush(c)
            c.setAlphaF(0.3)
            p.setPen(gui.QPen(c, 2.0))
            scale = 1.0 / self._document_length
            if self.orientation() == constants.VERTICAL:
                rects = [
                    core.QRect(
                        x + 1,
                        y + h * start * scale - 0.5,
                        w - 2,
                        h * (end - start) * scale + 1,
                    )
                    for start, end in self._annotations
                ]
            else:
                rects = [
                    core.QRect(
                        x + w * start * scale - 0.5,
                        y + 1,
                        w * (end - start) * scale + 1,
                        h - 2,
                    )
                    for start, end in self._annotations
                ]
            p.drawRects(rects)

    def set_annotation_color(self, color: datatypes.ColorType):
        self._annotation_color = colors.get_color(color).as_qt()

    def get_annotation_color(self) -> gui.QColor:
        return self._annotation_color

    def set_document_length(self, length: int):
        self._document_length = length

    def get_document_length(self) -> int:
        return self._document_length

    annotation_color = core.Property(
        gui.QColor,
        get_annotation_color,
        set_annotation_color,
        doc="Color for the annotated regions",
    )
    annotations = core.Property(
        list,
        get_annotations,
        set_annotations,
        doc="Current set of annotations",
    )
    document_length = core.Property(
        int,
        get_document_length,
        set_document_length,
        doc="Total document length",
    )

⌗ 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
minimum int
maximum int
singleStep int
pageStep int
value int
sliderPosition int
tracking bool
orientation Qt::Orientation
invertedAppearance bool
invertedControls bool
sliderDown bool
annotation_color QColor Color for the annotated regions
annotations QVariantList Current set of annotations
document_length int Total document length