#!/usr/bin/python
# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
class grafikWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(grafikWidget, self).__init__(parent)
model = QtGui.QStandardItemModel()
list = [u'1 колонка', u'2 колонка', u'3 колонка', u'4 колонка', u'5 колонка']
model.setHorizontalHeaderLabels(list)
for i in range (0,10):
for j in range (0,5):
newItem = QtGui.QStandardItem('#%d %d'%(i,j))
model.setItem(i,j, newItem)
tableView = FreezeTableWidget(model)
gl = QtGui.QGridLayout(self)
gl.addWidget(tableView,0,0)
class FreezeTableWidget(QtGui.QTableView):
def __init__(self, model, parent=None):
super(QtGui.QTableView, self).__init__(parent)
self.setModel(model)
self.frozenTableView = QtGui.QTableView(self)
self.init()
self.connect(self.horizontalHeader(),QtCore.SIGNAL('sectionResized(int,int,int)'), self.updateSectionWidth)
self.connect(self.verticalHeader(),QtCore.SIGNAL('sectionResized(int,int,int)'), self.updateSectionHeight)
self.connect(self.frozenTableView.verticalScrollBar(), QtCore.SIGNAL('valueChanged(int)'),
self.verticalScrollBar(), QtCore.SLOT('setValue(int)'))
self.connect(self.verticalScrollBar(), QtCore.SIGNAL('valueChanged(int)'),
self.frozenTableView.verticalScrollBar(), QtCore.SLOT('setValue(int)'))
def init(self):
self.frozenTableView.setModel(self.model())
self.frozenTableView.setFocusPolicy(QtCore.Qt.NoFocus)
self.frozenTableView.verticalHeader().hide()
self.frozenTableView.horizontalHeader().setResizeMode(QtGui.QHeaderView.Fixed)
self.viewport().stackUnder(self.frozenTableView)
self.frozenTableView.setStyleSheet("QTableView { border: none;"
"background-color: #8EDE21;"
"selection-background-color: #999}")
self.frozenTableView.setSelectionModel(self.selectionModel())
for col in range(1,self.model().columnCount()):
self.frozenTableView.setColumnHidden(col, True)
self.frozenTableView.setColumnWidth(0, self.columnWidth(0))
self.frozenTableView.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.frozenTableView.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.frozenTableView.show()
self.updateFrozenTableGeometry()
self.setHorizontalScrollMode(self.ScrollPerPixel)
self.setVerticalScrollMode(self.ScrollPerPixel)
self.frozenTableView.setVerticalScrollMode(self.ScrollPerPixel)
def updateSectionWidth(self,logicalIndex,i,newSize):
if logicalIndex==0:
self.frozenTableView.setColumnWidth(0,newSize)
self.updateFrozenTableGeometry()
def updateSectionHeight(self, logicalIndex, i, newSize):
self.frozenTableView.setRowHeight(logicalIndex, newSize)
def resizeEvent(self,event):
super(QtGui.QTableView, self).resizeEvent(event)
self.updateFrozenTableGeometry()
def moveCursor(self, cursorAction, modifiers):
current = QtGui.QTableView.moveCursor(self, cursorAction, modifiers)
if (cursorAction == self.MoveLeft and current.column()>0
and self.visualRect(current).topLeft().x() < self.frozenTableView.columnWidth(0) ):
newValue = self.horizontalScrollBar().value() + self.visualRect(current).topLeft().x() - self.frozenTableView.columnWidth(0)
self.horizontalScrollBar().setValue(newValue)
return current
def scrollTo(self, index, hint):
if (index.column()>0):
QtGui.QTableView.scrollTo(self, index, hint)
def updateFrozenTableGeometry(self):
self.frozenTableView.setGeometry(self.verticalHeader().width() + self.frameWidth(),
self.frameWidth(),
self.columnWidth(0),
self.viewport().height()+self.horizontalHeader().height())