Skip to content

Commit fe728b6

Browse files
committed
Correctly calculate number of csv rows to delete
CSV Editor would delete too many rows if multiple columns where selected. This fix correctly calculates how many rows are selected when deleting rows. Fixes #13
1 parent b70c5f2 commit fe728b6

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

Chapter05/csv_editor.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,17 @@ def insert_below(self):
152152

153153
def remove_rows(self):
154154
selected = self.tableview.selectedIndexes()
155+
# Errata: The book contains the following code:
156+
#if selected:
157+
# self.model.removeRows(selected[0].row(), len(selected), None)
158+
159+
# This is incorrect, as len(selected) is the number of *cells* selected,
160+
# not the number of *rows* selected.
161+
162+
# correct approach would look like this:
163+
num_rows = len(set(index.row() for index in selected))
155164
if selected:
156-
self.model.removeRows(selected[0].row(), len(selected), None)
165+
self.model.removeRows(selected[0].row(), num_rows, None)
157166

158167

159168
if __name__ == '__main__':

0 commit comments

Comments
 (0)