class StatusScrollArea(QScrollArea): def __init__(self): super(StatusScrollArea, self).__init__() self._width = 0 self._group_count = 0 self._pan_pos = None self.__create_ui() self.__init_ui() def __create_ui(self): self.container = QWidget() self.container_layout = QHBoxLayout() self.container.setLayout(self.container_layout) self.setWidget(self.container) def __init_ui(self): self.container.setFixedHeight(TOOLBAR_BUTTON_SIZE) self.container_layout.setContentsMargins(0, 0, 0, 0) self.container_layout.setSpacing(1) self.container_layout.setAlignment(Qt.AlignLeft) self.setFixedHeight(TOOLBAR_BUTTON_SIZE) self.setFocusPolicy(Qt.NoFocus) self.setFrameShape(self.NoFrame) self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) def _update_width(self, expand_value): self._width += expand_value self.container.setFixedWidth(self._width + self._group_count) def mousePressEvent(self, event): if event.button() == Qt.MidButton: QApplication.setOverrideCursor(QCursor(Qt.SizeHorCursor)) self._pan_pos = event.globalPos() event.accept() else: event.ignore() def mouseReleaseEvent(self, event): if event.button() == Qt.MidButton: QApplication.restoreOverrideCursor() self._pan_pos = None event.accept() else: event.ignore() def mouseMoveEvent(self, event): if self._pan_pos: h_bar = self.horizontalScrollBar() h_bar_pos = h_bar.sliderPosition() cursor_pos = event.globalPos() cursor_delta = (cursor_pos - self._pan_pos).x() h_bar.setValue(h_bar_pos - cursor_delta) self._pan_pos = cursor_pos event.accept() else: event.ignore() def wheelEvent(self, event): if event.orientation() == Qt.Vertical: num_degrees = event.delta() / 8 h_bar = self.horizontalScrollBar() h_bar_pos = h_bar.sliderPosition() h_bar.setValue(h_bar_pos - num_degrees) else: super(StatusScrollArea, self).wheelEvent(event) def resizeEvent(self, event): max_scroll = max(0, self.container.width() - event.size().width()) self.horizontalScrollBar().setMaximum(max_scroll) def add_widget(self, widget): # add widget to layout self.container_layout.addWidget(widget) # connect widget for future update when user interact with it widget.toggled.connect(self._update_width) # expand widget layout self._width += widget.max_length() self._group_count += 1 self.container.setFixedWidth(self._width)