Hi,

the attached script exhibits a strange behaviour which can be reproduced as 
follows:

- run the script and enlarge the table. Keep the vertical scrollbar visible
- select a range of cells with the mouse, for instance top_left=(0,2) and
bottom_right=(2,3).
- now drag the slider downwards until the selection disappears *completely* 
and upwards until it appears again
- now click on any unselected cell (or select a new range of cells). By doing 
it I expected that the complete selection would be cleared and the clicked 
cell would become current and selected. Instead only the previous current cell 
is deselected. The rest of the old selection remains selected and also the new 
clicked cell is selected and current
- repeat the previous step as many times as you want. The behaviour is always 
the same

Is this a bug or am I doing something wrong?

I'm using PyQt4.8.3 and Python 2.7 on a Debian testing box.

TIA,

Vicent

::

        Share what you know, learn what you don't

# -*- coding: utf-8 -*-
#!/usr/bin/env python

import sys
from PyQt4 import QtCore, QtGui

class TableModel(QtCore.QAbstractTableModel):
    def __init__(self, parent=None):
        super(TableModel, self).__init__(parent)
        self.nrows = 30
        self.ncols = 4

    def rowCount(self, index=QtCore.QModelIndex()):
        if not index.isValid():
          return self.nrows
        else:
          return 0

    def columnCount(self, index=QtCore.QModelIndex()):
        if not index.isValid():
          return self.ncols
        else:
          return 0

    def data(self, index, role=QtCore.Qt.DisplayRole):
        if not index.isValid() or not (0 <= index.row() < self.nrows):
            return None
        cell = index.row() + index.column()
        if role == QtCore.Qt.DisplayRole:
            return cell
        elif role == QtCore.Qt.TextAlignmentRole:
            return int(QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)
        else:
            return None


class TableView(QtGui.QTableView):
    def __init__(self, parent=None):
        super(TableView, self).__init__(parent)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.tmodel = TableModel()
        self.setModel(self.tmodel)
        self.selection_model = self.selectionModel()
        self.setSelectionMode(QtGui.QAbstractItemView.ContiguousSelection)
        self.vscrollbar = self.verticalScrollBar()

        self.selection_model.selectionChanged.connect(self.trackSelection)
        self.vscrollbar.sliderMoved.connect(self.updateView)

    def updateView(self, *value):
        self.selection_model.selectionChanged.disconnect()
        self.selection_model.clearSelection()
        top_left = self.tmodel.index(self.top_left[0], self.top_left[1])
        bottom_right = self.tmodel.index(self.bottom_right[0], self.bottom_right[1])
        selection = QtGui.QItemSelection(top_left, bottom_right)
        self.selection_model.select(selection, QtGui.QItemSelectionModel.Select)
        self.selection_model.selectionChanged.connect(self.trackSelection)

    def trackSelection(self, selected, deselected):
        # Get the selection range of the selection model
        selection = self.selection_model.selectedIndexes()
        cells = [[i.row(), i.column()] for i in selection]
        self.top_left = min(cells)
        self.bottom_right = max(cells)


if __name__ == '__main__':
  app = QtGui.QApplication(sys.argv)
  view = TableView()
  view.show()
  app.exec_()

Attachment: signature.asc
Description: This is a digitally signed message part.

_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to