Page 1 of 2 12 LastLast
Results 1 to 20 of 24

Thread: Possible conversion from QStandardItemModel to QAbstractTableModel?

  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 19: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)

  6. #5
    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 d_stranz! I am currently working on doing just what you both have recommended - hopefully to express some results shortly.
    Looking forward,

    EDIT: Added information

    Something seems to be in the right order.
    With downward scrolling, the row count (and scroll space) increases. These changes also express quicker load time then previous.
    However this haste could be due to the following woes.

    Although the initial connection between the QAbstractTableModel and QtableView seems to be working, the table is not getting populated with row data.
    Qt Code:
    1. class CSVModel(QtCore.QAbstractTableModel):
    2. activeRows = 35
    3. def __init__(self, fileName, parent=None):
    4. super(CSVModel,self).__init__()
    5. header = []
    6. CSVarray = []
    7. fileName = r'E:\Documents\SIUC\2014\Fall\440 - Hydro\QGIS\test_bay\CSVtesting\mfLayer1_Grid.csv'
    8. with open(fileName, "rb") as fileInput:
    9. for idx, row in enumerate(csv.reader(fileInput)):
    10. headerIDx = 0
    11. if idx is headerIDx:
    12. header.append(row)
    13. elif idx>headerIDx:
    14. items = [field for field in row]
    15. CSVarray.append(items)
    16. self.header = header
    17. self.rows = CSVarray
    18. self.rowsLoaded = CSVModel.activeRows
    19. def rowCount(self,index):
    20. if not self.rows:
    21. return 0
    22. if len(self.rows) <= self.rowsLoaded:
    23. return len(self.rows)
    24. else:
    25. return self.rowsLoaded
    26. def canFetchMore(self,index):
    27. if len(self.rows) > self.rowsLoaded:
    28. return True
    29. else:
    30. return False
    31. def fetchMore(self,index):
    32. reminder = len(self.rows) - self.rowsLoaded
    33. itemsToFetch = min(reminder,CSVModel.activeRows)
    34. self.beginInsertRows(index,self.rowsLoaded,(self.rowsLoaded+itemsToFetch-1))
    35. self.rowsLoaded += itemsToFetch
    36. self.endInsertRows()
    37. def addRow(self,row):
    38. self.beginResetModel()
    39. for i in self.CSVarray:
    40. self.rows.append(i)
    41. self.endResetModel()
    42. def columnCount(self,index):
    43. return len(self.header[0])
    44. def data(self,index,role=Qt.DisplayRole):
    45. col = index.column()
    46. row = self.rows[index.row()]
    47. if role == Qt.DisplayRole:
    48. if col == 0:
    49. return str('meow 1')
    50. elif col == 1:
    51. return str('meow 2')
    52. return
    53. def headerData(self,section,orientation,role=Qt.DisplayRole):
    54. if role != Qt.DisplayRole:
    55. return
    56. if orientation == Qt.Horizontal:
    57. return self.header[0][section]
    58. return int(section+1)
    To copy to clipboard, switch view to plain text mode 
    blankTable.jpg
    Last edited by jkrienert; 24th December 2014 at 21:26.

  7. #6
    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?

    Ah, your problem is most likely lines 49 and 51 of your code. Everyone knows that you can't get cats to do anything you want them to. You should have tried it with dogs instead...

    I'll think on this code and see if I can see a real problem. Not a Python expert, but code is code.

    Edit: If I understand the code, it looks like you are opening your CSV file in binary mode ("rb"). Is that what your csvReader expects?
    Further edit: Never mind. You are getting the headers, so that part is OK.
    Last edited by d_stranz; 24th December 2014 at 22:14.

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

    jkrienert (25th December 2014)

  9. #7
    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?

    I am not an expert in Python, but aren't you missing one return in your data method?

    Also make sure it returns a QVariant instance.

    Cheers,
    _

  10. The following user says thank you to anda_skoa for this useful post:

    jkrienert (25th December 2014)

  11. #8
    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?

    To d_stranz:
    Meow?
    [working on 44 -52 presently]

    As far as my (novice) knowledge goes - the 'r' designates a read operation, and the 'b' is a platform denotation that the csv is an 'object'. But as you mentioned, the 'b' is referring to binary formatting?
    ...You might have a point:

    http://stackoverflow.com/questions/1...-in-csv-module

    Removing the 'b' didn't present any difference in current operation, and this sections ('rb') presence in the original QStandardItemModel caused no errors - so I suppose it might be ok... (reference image of table in 'standard' below)
    columnSelect.jpg
    Thanks for taking the time to birds-eye this code. I would be happy to post the whole enchilada - if it would help.
    There are several portions of the code which need resorting. For example, mouseRelease event to update a variable for application to all selected 'cells' in the table. Such is better left for another day until overcoming the current.



    To anda_skoa:
    Your right about that return!
    For some reason (be it the version of Python3 or Qt4) I am unable to get functionality from QVariant instances.
    This could be due to (my) syntax errors though...

    To all:
    This bit doesn't seem to work either ... It must not be barking like a dog should (@d_stranz)
    Qt Code:
    1. def data(self,index,role):
    2. col = index.column()
    3. row = self.rows[index.row()]
    4. if role == Qt.DisplayRole:
    5. for idx, i in enumerate(self.header):
    6. if col == idx:
    7. return row[idx]
    To copy to clipboard, switch view to plain text mode 

    update
    Hazah!
    Qt Code:
    1. def data(self,index,role):
    2. col = index.column()
    3. row = self.rows[index.row()]
    4. if role == Qt.DisplayRole:
    5. return row[col]
    To copy to clipboard, switch view to plain text mode 
    Table now populates but editing is not possible.
    Probably much is needed to enable such...
    yippe.jpg
    Last edited by jkrienert; 24th December 2014 at 22:56.

  12. #9
    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?

    Quote Originally Posted by jkrienert View Post
    Table now populates but editing is not possible.
    Probably much is needed to enable such...
    Implement flags() to make the cell's editable and setData() to take the modified data and store it.
    Emit dataChanged() when the data actually changed.

    Cheers,
    _

  13. The following user says thank you to anda_skoa for this useful post:

    jkrienert (25th December 2014)

  14. #10
    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?

    Implement flags() ... and setData() to take the modified data and store it.
    These aspects were successfully incorporated (thank you!) - yet moving forward to enabling a proper update of the original CSV has run into some snags.

    The vertical header (row numbers) is properly associated with the total number of rows in the AbstractTableModel, but the entire tables data is 'static' to the last columns value starting at the furthest extent of the initial QtableView load-up [35 rows] (picture below might be more descriptive).
    Possibly due to conflict of the CSVwriter function (within the QtableView class), and syntax errors?

    writeWoes.jpg

    Also, is there anyway to start the row index represented in the vertical header at 0 rather then 1? It has an association with IDs in the CSV dataset, and would be useful to keep in sync.


    Writer function within QtableView Class:

    Qt Code:
    1. def writeCsv(self, fileName):
    2. with open(fileName, "wb") as fileOutput:
    3. writer = csv.writer(fileOutput)
    4. writer.writerow(self.horizHeaders)
    5. for rowNumber in range(len(self.tableData.rows)):
    6. fields = [self.tableData.data(self.tableData.index(rowNumber,columnNumber),QtCore.Qt.DisplayRole) for columnNumber in range(self.tableData.columnCount(self))]
    7. writer.writerow(fields)
    8. print ':)'
    To copy to clipboard, switch view to plain text mode 

  15. #11
    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?

    Quote Originally Posted by jkrienert View Post
    The vertical header (row numbers) is properly associated with the total number of rows in the AbstractTableModel, but the entire tables data is 'static' to the last columns value starting at the furthest extent of the initial QtableView load-up [35 rows] (picture below might be more descriptive).
    Possibly due to conflict of the CSVwriter function (within the QtableView class), and syntax errors?
    Have you checked the array?
    Does it contain other values for these indexes?

    Quote Originally Posted by jkrienert View Post
    Also, is there anyway to start the row index represented in the vertical header at 0 rather then 1? It has an association with IDs in the CSV dataset, and would be useful to keep in sync.
    You just need to implement the "orientation is vertical" branch of headerData()

    Cheers,
    _

  16. #12
    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?

    Indeed the CSVarray, which is referenced via 'self.rows' is a full dataset of the CSV source.

    The image I posted regarding the save error was a CSV reopened in the editor.
    Ergo, the extent to which you scroll in the initial open CSV is the maximum edits saved to the revised CSV.
    for example, when I scrolled halfway through the total rows, the saved CSV has proper data up to that point (halfway), the rest being '98' duplicates of the value in the last column.
    If I don't scroll at all, the only saved updates are to the last row visible (loaded) in the table window.

    I'm inclined to believe that maybe this has to do with the relationship (position) of the CSVwriter from the AbstractTableModel?


    implement the "orientation is vertical" branch of headerData()
    Success!
    Qt Code:
    1. def headerData(self,section,orientation,role=Qt.DisplayRole):
    2. if role != Qt.DisplayRole:
    3. return
    4. if orientation == Qt.Vertical:
    5. return int(section)
    6. if orientation == Qt.Horizontal:
    7. return self.header[0][section]
    To copy to clipboard, switch view to plain text mode 

  17. #13
    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?

    Quote Originally Posted by jkrienert View Post
    I'm inclined to believe that maybe this has to do with the relationship (position) of the CSVwriter from the AbstractTableModel?
    I think your problem is the asymmetry in how you handle I/O.

    You do the loading inside the model and the writing outside.

    Maybe put the saving into the model as well, and operate on the actual data instead of going through the abstraction that only the view needs?

    For inspiration have a look at QSqlTableModel.
    It also provide read/write access to tabular data and also implements load/save from/to backend, with different strategies for different behavioral trade-offs. Your target might be something like its OnManualSubmit strategy

    Cheers,
    _

  18. The following user says thank you to anda_skoa for this useful post:

    jkrienert (26th December 2014)

  19. #14
    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?

    You do the loading inside the model and the writing outside.
    Yes, exactly. QTableView will load rows only as needed (eg. when they become visible through scrolling or programmatically). So, since your save method is based on what is contained in the view, that's all that will be written out.

    As anda_skoa said, you need to move the save code so it works off the model itself and not the view of it.

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

    jkrienert (26th December 2014)

  21. #15
    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?

    Your advice makes sence; emplace the 'save' function within the model class to ensure write of the full data contents.
    However, my first take on such reveals no call of the save function from a button click in the View class:

    View Class button references:
    Qt Code:
    1. def __init__(...)
    2. ...
    3. self.pushButtonSave = QtGui.QPushButton(self)
    4. self.pushButtonSave.setText("Save Changes")
    5. self.pushButtonSave.clicked.connect(self.on_pushButtonSave_clicked)
    6.  
    7. ...
    8.  
    9. @QtCore.pyqtSlot()
    10. def on_pushButtonSave_clicked(self):
    11. print 'meow 1'
    12. CSVModel.saveIt
    To copy to clipboard, switch view to plain text mode 

    Model Class save function:
    Qt Code:
    1. def saveIt(self):
    2. print 'meow 2'
    3. with open(self.fileName, "wb") as fileOutput:
    4. writer = csv.writer(fileOutput)
    5. writer.writerow(self.header)
    6. for rowNumber in range(len(self.rows)):
    7. fields = [self.data(self.index(rowNumber,columnNumber),QtCore.Qt.DisplayRole) for columnNumber in range(self.columnCount(self))]
    8. writer.writerow(fields)
    9. print 'happy cat'
    To copy to clipboard, switch view to plain text mode 

    While I do get a return for 'meow 1', 'meow 2' never shows. This occurs without error prompt(s), exhibiting a situation as if the save function content simply read 'pass'.






    UPDATE:

    Added after 1hr:


    Turns out the View Class 'on_pushButtonSave_clicked' was a bit mixed up. This got things working:
    Qt Code:
    1. @QtCore.pyqtSlot()
    2. def on_pushButtonSave_clicked(self):
    3. CSVModel.saveIt(CSVModel(self))
    To copy to clipboard, switch view to plain text mode 

    Although, now it seems my 'saveIt' function in the Model Class is a bit screwy. Error lays in the row write statements [line 6]:
    Qt Code:
    1. def saveIt(self):
    2. with open(self.fileName, "wb") as fileOutput:
    3. writer = csv.writer(fileOutput)
    4. writer.writerow(self.header[0])
    5. for rowNumber in range(len(self.rows)):
    6. fields = [self.data(self.index(rowNumber,columnNumber),QtCore.Qt.DisplayRole) for columnNumber in range(self.columnCount(self))]
    7. writer.writerow(fields)
    To copy to clipboard, switch view to plain text mode 
    The same problem as previous is exhibited (partial write-out of data ~ see below) when reopening the CSV file. Working on this now...
    ongoing.jpg
    Last edited by jkrienert; 26th December 2014 at 16:08.

  22. #16
    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?

    Quote Originally Posted by jkrienert View Post
    The same problem as previous is exhibited (partial write-out of data ~ see below) when reopening the CSV file. Working on this now...
    You still access the data through the model's data() method, might be more straight forward to use self.rows.

    Cheers,
    _

  23. The following user says thank you to anda_skoa for this useful post:

    jkrienert (28th December 2014)

  24. #17
    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?

    For simplicitys sake, I agree - but it seems I might be making variable associations improperly or some other reference error.
    When using the following, I get a fully saved CSV - yet only displaying the original values, without any of the 'applied' edits:
    Qt Code:
    1. def saveIt(self):
    2. with open(self.fileName, "wb") as fileOutput:
    3. writer = csv.writer(fileOutput)
    4. writer.writerow(self.header[0])
    5. for rowNumber in range(len(self.rows)):
    6. fields = self.rows[rowNumber]
    7. writer.writerow(fields)
    8. print ':)'
    To copy to clipboard, switch view to plain text mode 


    I have a feeling the mishap is spawned in this reference earlier in the AbstractModel:
    Qt Code:
    1. header = []
    2. CSVarray = []
    3. self.fileName = r'E:\Documents\SIUC\2014\Fall\440 - Hydro\QGIS\test_bay\CSVtesting\mfLayer1_Grid.csv'
    4. with open(self.fileName, "rb") as fileInput:
    5. for idx, row in enumerate(csv.reader(fileInput)):
    6. headerIDx = 0
    7. if idx is headerIDx:
    8. header.append(row)
    9. elif idx>headerIDx:
    10. items = [field for field in row]
    11. CSVarray.append(items)
    12. self.header = header
    13. self.rows = CSVarray
    14. self.rowsLoaded = CSVModel.activeRows
    To copy to clipboard, switch view to plain text mode 


    I wonder my error occurs in sending user updates to the array data?
    Last edited by jkrienert; 27th December 2014 at 14:01.

  25. #18
    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?

    How does your setData() look like?

    Are you writing into self.rows there?

    Cheers,
    _

  26. The following user says thank you to anda_skoa for this useful post:

    jkrienert (28th December 2014)

  27. #19
    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?

    For fullness sake:
    Qt Code:
    1. class CSVModel(QtCore.QAbstractTableModel):
    2. activeRows = 35
    3. def __init__(self, fileName, parent=None):
    4. super(CSVModel,self).__init__()
    5. header = []
    6. CSVarray = []
    7. self.fileName = r'E:\Documents\SIUC\2014\Fall\440 - Hydro\QGIS\test_bay\CSVtesting\mfLayer1_Grid.csv'
    8. with open(self.fileName, "rb") as fileInput:
    9. for idx, row in enumerate(csv.reader(fileInput)):
    10. headerIDx = 0
    11. if idx is headerIDx:
    12. header.append(row)
    13. elif idx>headerIDx:
    14. items = [field for field in row]
    15. CSVarray.append(items)
    16. self.header = header
    17. self.rows = CSVarray
    18. self.rowsLoaded = CSVModel.activeRows
    19.  
    20. def rowCount(self,index):
    21. if not self.rows:
    22. return 0
    23. if len(self.rows) <= self.rowsLoaded:
    24. return len(self.rows)
    25. else:
    26. return self.rowsLoaded
    27. def canFetchMore(self,index):
    28. if len(self.rows) > self.rowsLoaded:
    29. return True
    30. else:
    31. return False
    32. def fetchMore(self,index):
    33. reminder = len(self.rows) - self.rowsLoaded
    34. itemsToFetch = min(reminder,CSVModel.activeRows)
    35. self.beginInsertRows(index,self.rowsLoaded,(self.rowsLoaded+itemsToFetch-1))
    36. self.rowsLoaded += itemsToFetch
    37. self.endInsertRows()
    38. def addRow(self,row):
    39. self.beginResetModel()
    40. self.rows.append(row)
    41. self.endResetModel()
    42. def columnCount(self,index):
    43. return len(self.header[0])
    44. def data(self,index,role):
    45. col = index.column()
    46. row = self.rows[index.row()]
    47. if role == Qt.DisplayRole:
    48. return row[col]
    49. def headerData(self,section,orientation,role):
    50. if role != Qt.DisplayRole:
    51. return
    52. if orientation == Qt.Vertical:
    53. return int(section)
    54. if orientation == Qt.Horizontal:
    55. return self.header[0][section]
    56. def setData(self, index, value, role):
    57. print value
    58. if role == Qt.EditRole:
    59. col = index.column()
    60. row = self.rows[index.row()]
    61. row[col]=value
    62. return True
    63. return False
    64. def flags(self, index):
    65. return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
    66. def saveIt(self):
    67. with open(self.fileName, "wb") as fileOutput:
    68. writer = csv.writer(fileOutput)
    69. writer.writerow(self.header[0])
    70. for rowNumber in range(len(self.rows)):
    71. fields = self.rows[rowNumber]
    72. writer.writerow(fields)
    73. print ':)'
    To copy to clipboard, switch view to plain text mode 
    Last edited by jkrienert; 27th December 2014 at 15:45. Reason: Grammer error

  28. #20
    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?

    yes, dataChanged() should be emitted, but that should not impact the saving.

    Does line 60 get a reference to the row or does it create a copy?

    If you call data() at the end of setData(), does it return the new value?

    Cheers,
    _

  29. The following user says thank you to anda_skoa for this useful post:

    jkrienert (28th December 2014)

Similar Threads

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