简体   繁体   中英

How many rows or columns can QTableWidget have?

I want to make a spreadsheet like Excel. Excel 2010 has 1048576 rows and A - ZZZ columns. But in Qt, the application crashes if it has more than about 7000~8000 rows.

Is it a bug, or is it the default settings of Qt? Sure, this crash happens at the lower versions. Do you run into the same problem?

Sample Code:

from PySide2 import QtWidgets
import PySide2
import os

dirname = os.path.dirname(PySide2.__file__)
plugin_path = os.path.join(dirname, 'plugins', 'platforms')
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path
import sys
import itertools
alphabet = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
alphabet2 =[i[0]+i[1] for i in itertools.product(alphabet, alphabet)]
alphabet3 =[i[0]+i[1] for i in itertools.product(alphabet, alphabet2)]
alphabet = alphabet + alphabet2 + alphabet3
EXCEL2010_ROW_MAX = 1048576
NOW_POSSIBLE_MAX = 7000
def main():
    app = QtWidgets.QApplication([])
    tablewidget = QtWidgets.QTableWidget()
    tablewidget.setRowCount(EXCEL2010_ROW_MAX)
    tablewidget.setColumnCount(len(alphabet))
    tablewidget.setHorizontalHeaderLabels(alphabet)    
    tablewidget.show()
    sys.exit(app.exec_())
if __name__ == "__main__":
    main()

This is not a bug - your computer is simply running out of memory.

Your example code creates a table with over 19 billion cells ( (26¹ + 26² + 26³) * 1048576 = 19165872128 ). So, unless your computer has several dozen gigabytes of memory available, it won't be able to load it. It seems safe to assume Excel doesn't attempt to render an actual table of that size, either. And even if it supports a virtual maximum size of 1048576 rows by 16384 columns, that doesn't mean you can fill every cell with data. You will always be limited by the amount of available memory (and disk space) on the system.

When you use item-based convenience classes like QTableWidget or QStandardItemModel , you cannot avoid these contraints, because too much memory needs to be allocated up front. So if you want to emulate Excel in Qt, you will need to use QTableView with a custom model. This allows you to create virtual tables of an arbitrary size, since Qt will only try to render the cells currently visible in the viewport.

Here is a simple demo:

import sys
from PySide2 import QtCore, QtWidgets

class CustomModel(QtCore.QAbstractItemModel):
    def rowCount(self, *args):
        return 1048576

    def columnCount(self, *args):
        return 163846

    def index(self, *args):
        return QtCore.QModelIndex()

app = QtWidgets.QApplication(sys.argv)
table = QtWidgets.QTableView()
model = CustomModel()
table.setModel(model)
table.show()
sys.exit(app.exec_())

See the Model Subclassing Reference for more details on how to implement custom models.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM