Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FreezeTableWidget: Add parent at __init__ and delayed setModel #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions examples/itemviews/frozencolumn/frozencolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,9 @@
from PyQt5.QtGui import QStandardItem, QStandardItemModel
from PyQt5.QtWidgets import QApplication, QHeaderView, QTableView


class FreezeTableWidget(QTableView):
def __init__(self, model):
super(FreezeTableWidget, self).__init__()
self.setModel(model)
def __init__(self, parent):
super(FreezeTableWidget, self).__init__(parent)
self.frozenTableView = QTableView(self)
self.init()
self.horizontalHeader().sectionResized.connect(self.updateSectionWidth)
Expand All @@ -69,8 +67,14 @@ def __init__(self, model):
self.verticalScrollBar().valueChanged.connect(
self.frozenTableView.verticalScrollBar().setValue)

def setModel(self, model):
super(FreezeTableWidget, self).setModel(model)
self.frozenTableView.setModel(model)
for col in range(1, model.columnCount()):
self.frozenTableView.setColumnHidden(col, True)
self.frozenTableView.setSelectionModel(self.selectionModel())

def init(self):
self.frozenTableView.setModel(self.model())
self.frozenTableView.setFocusPolicy(Qt.NoFocus)
self.frozenTableView.verticalHeader().hide()
self.frozenTableView.horizontalHeader().setSectionResizeMode(
Expand All @@ -83,9 +87,6 @@ def init(self):
selection-background-color: #999;
}''') # for demo purposes

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(Qt.ScrollBarAlwaysOff)
self.frozenTableView.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
Expand All @@ -96,7 +97,7 @@ def init(self):
self.frozenTableView.setVerticalScrollMode(self.ScrollPerPixel)

def updateSectionWidth(self, logicalIndex, oldSize, newSize):
if self.logicalIndex == 0:
if logicalIndex == 0:
self.frozenTableView.setColumnWidth(0, newSize)
self.updateFrozenTableGeometry()

Expand All @@ -108,7 +109,8 @@ def resizeEvent(self, event):
self.updateFrozenTableGeometry()

def moveCursor(self, cursorAction, modifiers):
current = super(FreezeTableWidget, self).moveCursor(cursorAction, modifiers)
current = super(FreezeTableWidget, self).moveCursor(cursorAction,
modifiers)
if (cursorAction == self.MoveLeft and
self.current.column() > 0 and
self.visualRect(current).topLeft().x() <
Expand All @@ -132,7 +134,7 @@ def updateFrozenTableGeometry(self):

def main(args):
def split_and_strip(s, splitter):
return [s.strip() for s in line.split(splitter)]
return [l.strip() for l in s.split(splitter)]

app = QApplication(args)
model = QStandardItemModel()
Expand Down