Results 1 to 20 of 24

Thread: Possible conversion from QStandardItemModel to QAbstractTableModel?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2014
    Posts
    48
    Thanks
    23
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Windows

    Default Possible conversion from QStandardItemModel to QAbstractTableModel?

    At the moment a QStandardItemModel is being used to support QTableView, but a future need to process large CSV files with > 50,000 rows + facilitates the need for an alternative method.

    I have read several great PyQt based articles on the subject (see below) of using a QAbstractTableModel to hold the data which will then provide QtableView with items 'on demand', yet novice programing skills are making the transition anything but progressive.

    http://www.saltycrane.com/blog/2007/...delqtableview/
    http://sateeshkumarb.wordpress.com/2...-data-in-pyqt/
    https://gist.githubusercontent.com/M...09/combobox.py

    Below are the primary bits of code (currently without QAbstractTableModel implemented) called by another piece of software, in all their existing error glory.

    Qt Code:
    1. import csv
    2. import sys
    3. from PyQt4 import QtGui, QtCore
    4. from PyQt4.QtCore import Qt, QVariant
    5. global xLoc
    6. global yLoc
    7. global itemIdxs
    8.  
    9. class CSVeditor(QtGui.QDialog, QtGui.QWidget):
    10. def __init__(self, fileName, horizHeaders, vertHeaders, parent=None):
    11. super(CSVeditor, self).__init__(parent)
    12.  
    13. self.horizHeaders = horizHeaders
    14. self.fileName = fileName
    15. self.model = QtGui.QStandardItemModel(self)
    16. self.model.setHorizontalHeaderLabels(horizHeaders)
    17.  
    18. self.tableView = QtGui.QTableView(self)
    19. self.tableView.setModel(self.model)
    20. self.tableView.horizontalHeader().setStretchLastSection(False)
    21. self.tableView.setAlternatingRowColors(True)
    22. self.tableView.hideColumn(0)
    23. self.tableView.hideColumn(1)
    24. self.tableView.hideColumn(2)
    25. self.tableView.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
    26. selectionModel = self.tableView.selectionModel()
    27. selectionModel.selectionChanged.connect(self.selRange)
    28. shortcut = QtGui.QShortcut(self)
    29. shortcut.setKey("Enter")
    30. self.connect(shortcut, QtCore.SIGNAL("activated()"), self.shareChanges)
    31. self.tableView.clicked.connect(self.Release)
    32.  
    33. # autoLoad CSV from workspace [to be edited for dropdown CSV upload selection(s)]
    34. with open(fileName, "rb") as fileInput:
    35. for idx, row in enumerate(csv.reader(fileInput)):
    36. header = 0
    37. if idx is header:
    38. pass
    39. elif idx>0:
    40. items = [QtGui.QStandardItem(field) for field in row]
    41. self.model.appendRow(items)
    42. self.model.setVerticalHeaderLabels(vertHeaders)
    43. self.show()
    44.  
    45. # index of selected range, and parent item (last index) for batch application of user-value changes
    46. def selRange(self,selected,deselected):
    47. selectedIdx = self.tableView.selectedIndexes()
    48. idxCol = []
    49. idxRow = []
    50. for row in selectedIdx:
    51. idxRow.append(row.row())
    52. for column in selectedIdx:
    53. idxCol.append(column.column())
    54. global itemIdxs
    55. itemIdxs = zip(idxRow,idxCol)
    56.  
    57. def shareChanges(self):
    58. self.tableView.clicked.connect(self.Release)
    59. try:
    60. updatedVal = self.model.data(self.model.index(xLoc,yLoc+3))
    61. print xLoc,yLoc,updatedVal
    62. except AttributeError:
    63. pass
    64. readOnlyCols = [0]
    65. try:
    66. for i in itemIdxs:
    67. row = i[0]
    68. col = i[1]
    69. if col in readOnlyCols:
    70. pass
    71. else:
    72. self.model.setData(self.model.index(row,col), updatedVal, Qt.DisplayRole)
    73. except AttributeError:
    74. print ":("
    75. pass
    76.  
    77. # button induced save-change to CSV (NO UNDO YET!)
    78. def writeCsv(self, fileName):
    79. with open(fileName, "wb") as fileOutput:
    80. writer = csv.writer(fileOutput)
    81. writer.writerow(self.horizHeaders)
    82. for rowNumber in range(self.model.rowCount()):
    83. fields = [self.model.data(self.model.index(rowNumber,columnNumber),QtCore.Qt.DisplayRole) for columnNumber in range(self.model.columnCount())]
    84. writer.writerow(fields)
    85. print ':)'
    86.  
    87. @QtCore.pyqtSlot()
    88. def on_pushButtonWrite_clicked(self):
    89. self.writeCsv(self.fileName)
    90.  
    91. def Cancel(self):
    92. self.close()
    93.  
    94. def closeEvent(self,event):
    95. self.deleteLater()
    96.  
    97. def Release(self,index):
    98. global xLoc
    99. global yLoc
    100. xLoc,yLoc = index.row(),(index.column()-3)
    To copy to clipboard, switch view to plain text mode 

    Is it feasible to chop shop existing, or start from scratch rather?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Possible conversion from QStandardItemModel to QAbstractTableModel?

    As a first step you could make a table model that holds all the data, e.g. in a 2-dimensional array.
    That should allow you to basically use the same loading code you are currently using.

    Cheers,
    _

  3. #3
    Join Date
    Dec 2014
    Posts
    48
    Thanks
    23
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Windows

    Default Re: Possible conversion from QStandardItemModel to QAbstractTableModel?

    Thanks for the quickness anda_skoa
    In this manner (?) :

    Qt Code:
    1. class CSVModel(QtCore.QAbstractTableModel):
    2. def __init__(self, CSVdata, fileName, parent = None):
    3. QAbstractTableModel.__init__(self, parent)
    4. self.CSVreader.rows = CSVdata
    5. def rowCount(self, parent):
    6. return len(self.CSVreader.rows)
    7. def columnCount(self, parent):
    8. return len(self.CSVreader.header)
    9. def data(self, index, role):
    10. self.CSVreader.rows[index.row()][index.column()]
    11. def CSVreader(self,fileName):
    12. header = []
    13. rows = []
    14. with open(fileName, "rb") as fileInput:
    15. for idx, row in enumerate(csv.reader(fileInput)):
    16. headIDx = 0
    17. if idx is headIDx:
    18. header.append(row)
    19. elif idx>headIDx:
    20. items = [field for field in row]
    21. rows.append(items)
    To copy to clipboard, switch view to plain text mode 
    Last edited by jkrienert; 24th December 2014 at 18:27. Reason: code error(s)

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Possible conversion from QStandardItemModel to QAbstractTableModel?

    but a future need to process large CSV files with > 50,000 rows + facilitates the need for an alternative method.
    Constructing QStandardItem instances for every cell in a table is a lot of overhead, so mapping your data from an internal data structure into a table model instead of using QStandardItemModel would be good.

    And as anda_skoa said, for the first step, simply implement the table model to hold the entire set of data. If this indeed kills performance, then go to plan B. Don't try to optimize before you know you have to.

  5. The following user says thank you to d_stranz for this useful post:

    jkrienert (24th December 2014)

Similar Threads

  1. Update QAbstractTableModel?
    By adutzu89 in forum Newbie
    Replies: 2
    Last Post: 4th June 2014, 21:17
  2. TableView and QAbstractTableModel
    By MattieB in forum Newbie
    Replies: 1
    Last Post: 11th December 2013, 20:57
  3. QAbstractTableModel and sort
    By foxyproxy in forum Qt Programming
    Replies: 7
    Last Post: 25th March 2008, 09:30
  4. Using QAbstractTableModel.parent
    By Max Yaffe in forum Newbie
    Replies: 7
    Last Post: 15th June 2007, 15:21
  5. QAbstractTableModel for QTreeView?
    By Michiel in forum Qt Programming
    Replies: 5
    Last Post: 15th May 2007, 09:09

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.