4

I need to display data using the PyQt class QTableWidget. But there could be dozens of thousand lines. Displaying the whole sheet makes the appli to hang...

So I found a solution consisting on loading data (meaning that the sheet is created) but hidding most of the rows and, when moving the scroll bar, show new rows and hidding previous ones.

In the bellow code, I setted self.rowsShown = 50 and at the init the lines from 1 to 50 are shown. And I previously did self.verticalScrollBar().actionTriggered.connect(self.refreshDisplay).

So the code is:

def refreshDisplay(self):
    """
    This is to refresh the display when the scrollbar is moved
    """

    # Minimum is reached and a previous row exists
    if self.verticalScrollBar().value() <= self.verticalScrollBar().minimum() and self.isRowHidden(self.firstRowShown-1):# Minimum is reached and a previous row exists
        for row in xrange(self.lastRowShown-1, self.lastRowShown):
            self.hideRow(row)
            self.lastRowShown -= 1
        for row in xrange(self.firstRowShown-1, self.firstRowShown):
            self.showRow(row)
            self.firstRowShown -= 1
    # Maximum is reached and a next row exists
    if self.verticalScrollBar().value() >= self.verticalScrollBar().maximum() and self.isRowHidden(self.lastRowShown+1):
        for row in xrange(self.firstRowShown, self.firstRowShown+1):
            self.hideRow(row)
            self.firstRowShown += 1
        for row in xrange(self.lastRowShown, self.lastRowShown+1):
            self.showRow(row)
            self.lastRowShown += 1

This is working very well when I use the roll of the mouse or click on buttons of the scrollbar. But when I grab the slider or use the keypad to move the case selected, I'm blocked in the area shown.

Moreover, the size of the scrolling area correspond to the rows shown only. I would like to redifine it to set my own bar (for example with a size based on the real number of lines), so that I can move fastly to another part of the sheet. But the size of the scroll bar is always updated with the number of lines shown.

So, what I expect from you is to help me to resolve this two issues, OR give me another idea for displaying such huge sheet. Maybe I missed a module or class that already bear this functionnality of loading/showing lines whilemoving the cursor.

Thanks

VGO
  • 2,161
  • 2
  • 17
  • 14
  • 1
    Have you tried [QTableView](http://qt-project.org/doc/qt-4.8/qtableview.html)? – alex vasi Aug 23 '12 at 13:49
  • `QTableWidget` inherits from `QTableView`. So, `hideRow()` and `showRow()` functions that I used here are from `QTableView`. – VGO Aug 23 '12 at 13:56

1 Answers1

2

I have written a tool that displays 10^6+ lines in an Excel-like table without resource problems. The solution is to use QTableView and QAbstractTableModel. You have to derive from QAbstractTableModel and implement the necessary functions it requires you to implement (I remember headerData and data, but I think there are more). You then plug the model into your view by doing view.setModel(model). More information how this is done can be found here.

hochl
  • 12,524
  • 10
  • 53
  • 87