Skip to content

Commit ae05ee9

Browse files
author
Pankaj Pandey
committed
Fix qt5 segfault in table editor test
QHeaderView section sizing can only be done when valid section exists, which mean only after it has a valid model.
1 parent 624666e commit ae05ee9

1 file changed

Lines changed: 47 additions & 39 deletions

File tree

‎traitsui/qt4/table_editor.py‎

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -857,45 +857,6 @@ def __init__(self, editor):
857857
self._editor = editor
858858
factory = editor.factory
859859

860-
# Configure the row headings.
861-
vheader = self.verticalHeader()
862-
insertable = factory.row_factory is not None and not factory.auto_add
863-
if ((factory.editable and (insertable or factory.deletable)) or
864-
factory.reorderable):
865-
vheader.installEventFilter(self)
866-
vheader.setResizeMode(QtWidgets.QHeaderView.ResizeToContents)
867-
elif not factory.show_row_labels:
868-
vheader.hide()
869-
self.setAlternatingRowColors(factory.alternate_bg_color)
870-
self.setHorizontalScrollMode(QtWidgets.QAbstractItemView.ScrollPerPixel)
871-
872-
# Configure the column headings.
873-
# We detect if there are any stretchy sections at all; if not, then
874-
# we make the last non-fixed-size column stretchy.
875-
hheader = self.horizontalHeader()
876-
resize_mode_map = dict(interactive = QtWidgets.QHeaderView.Interactive,
877-
fixed = QtWidgets.QHeaderView.Fixed,
878-
stretch = QtWidgets.QHeaderView.Stretch,
879-
resize_to_contents = QtWidgets.QHeaderView.ResizeToContents)
880-
stretchable_columns = []
881-
for i, column in enumerate(editor.columns):
882-
hheader.setResizeMode(i, resize_mode_map[column.resize_mode])
883-
if column.resize_mode in ("stretch", "interactive"):
884-
stretchable_columns.append(i)
885-
if not stretchable_columns:
886-
# Use the behavior from before the "resize_mode" trait was added
887-
# to TableColumn
888-
hheader.setStretchLastSection(True)
889-
else:
890-
hheader.setResizeMode(
891-
stretchable_columns[-1], QtWidgets.QHeaderView.Stretch)
892-
hheader.setStretchLastSection(False)
893-
894-
if factory.show_column_labels:
895-
hheader.setHighlightSections(False)
896-
else:
897-
hheader.hide()
898-
899860
# Configure the grid lines.
900861
self.setShowGrid(factory.show_lines)
901862

@@ -927,6 +888,10 @@ def __init__(self, editor):
927888

928889
self.resizeColumnsToContents()
929890

891+
def setModel(self, model):
892+
super(TableView, self).setModel(model)
893+
self._update_header_sizing()
894+
930895
def contextMenuEvent(self, event):
931896
"""Reimplemented to create context menus for cells and empty space."""
932897

@@ -1123,6 +1088,49 @@ def closeEditor(self, control, hint):
11231088

11241089
return super(TableView, self).closeEditor(control, hint)
11251090

1091+
def _update_header_sizing(self):
1092+
""" Header section sizing can be done only after a valid model is set.
1093+
Otherwise results in segfault with Qt5.
1094+
"""
1095+
editor = self._editor
1096+
factory = editor.factory
1097+
# Configure the row headings.
1098+
vheader = self.verticalHeader()
1099+
insertable = factory.row_factory is not None and not factory.auto_add
1100+
if ((factory.editable and (insertable or factory.deletable)) or
1101+
factory.reorderable):
1102+
vheader.installEventFilter(self)
1103+
vheader.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
1104+
elif not factory.show_row_labels:
1105+
vheader.hide()
1106+
self.setAlternatingRowColors(factory.alternate_bg_color)
1107+
self.setHorizontalScrollMode(QtWidgets.QAbstractItemView.ScrollPerPixel)
1108+
# Configure the column headings.
1109+
# We detect if there are any stretchy sections at all; if not, then
1110+
# we make the last non-fixed-size column stretchy.
1111+
hheader = self.horizontalHeader()
1112+
resize_mode_map = dict(interactive=QtWidgets.QHeaderView.Interactive,
1113+
fixed=QtWidgets.QHeaderView.Fixed,
1114+
stretch=QtWidgets.QHeaderView.Stretch,
1115+
resize_to_contents=QtWidgets.QHeaderView.ResizeToContents)
1116+
stretchable_columns = []
1117+
for i, column in enumerate(editor.columns):
1118+
hheader.setSectionResizeMode(i, resize_mode_map[column.resize_mode])
1119+
if column.resize_mode in ("stretch", "interactive"):
1120+
stretchable_columns.append(i)
1121+
if not stretchable_columns:
1122+
# Use the behavior from before the "resize_mode" trait was added
1123+
# to TableColumn
1124+
hheader.setStretchLastSection(True)
1125+
else:
1126+
# hheader.setSectionResizeMode(
1127+
# stretchable_columns[-1], QtWidgets.QHeaderView.Stretch)
1128+
hheader.setStretchLastSection(False)
1129+
if factory.show_column_labels:
1130+
hheader.setHighlightSections(False)
1131+
else:
1132+
hheader.hide()
1133+
11261134
#-------------------------------------------------------------------------
11271135
# Editor for configuring the filters available to a TableEditor:
11281136
#-------------------------------------------------------------------------

0 commit comments

Comments
 (0)