Hello World / plɹoM ollǝH

Programmers Live in Vain

QScrollAreaに固定されたヘッダーを表示する

f:id:dungeonneko:20170605143912g:plain

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_())

スライダーの移動量を固定する

f:id:dungeonneko:20170605163459g:plain

下記コードを追加する

def scrollControll(x):
    x = int(x / 128) * 128
    scroll.horizontalScrollBar().setValue(x)

scroll.horizontalScrollBar().setSingleStep(128)  # ボタン押された時の移動量
scroll.horizontalScrollBar().sliderMoved.connect(scrollControll)