QScrollAreaに固定されたヘッダーを表示する
import sys from PySide import QtGui class MyTable(QtGui.QWidget): def __init__(self): super().__init__() lo = QtGui.QHBoxLayout() lo.setContentsMargins(0, 0, 0, 0) lo.setSpacing(0) self.setLayout(lo) self._header = QtGui.QLabel('header') self._header.setStyleSheet('border-right: 1px solid black;\ border-bottom: 1px solid black;\ background-color: red;') self._header.setFixedSize(128, 32) lo.addWidget(self._header) for i in range(0, 8): item = QtGui.QLabel('cell {0}'.format(i)) item.setStyleSheet('border-right: 1px solid black;\ border-bottom: 1px solid black;\ background-color: white;') item.setFixedSize(128, 32) lo.addWidget(item) def setHeaderPos(self, x, y): self._header.move(x, y) self._header.raise_() app = QtGui.QApplication(sys.argv) table = MyTable() scroll = QtGui.QScrollArea() scroll.setWidget(table) def moveHeader(): x = scroll.horizontalScrollBar().value() table.setHeaderPos(x, 0) scroll.horizontalScrollBar().valueChanged.connect(moveHeader) scroll.show() sys.exit(app.exec_())
スライダーの移動量を固定する
下記コードを追加する
def scrollControll(x): x = int(x / 128) * 128 scroll.horizontalScrollBar().setValue(x) scroll.horizontalScrollBar().setSingleStep(128) # ボタン押された時の移動量 scroll.horizontalScrollBar().sliderMoved.connect(scrollControll)