1 Attachment(s)
Re: Possible conversion from QStandardItemModel to QAbstractTableModel?
It is odd, that printing 'self.rows' at the end of the setData fuction returns a list with proper updates, yet if printing 'self.rows' at the beginning of the saveIt fuction, the list does not show any of these updates (please see attached image at footer)
It seems maybe anda_skoa is right; in that possibly rather than reference - the current is creating a duplicate array...
Below are changes reflected to ensure that (previously line 60) is getting a reference and not a copy [hopefully its correct]:
Code:
def setData(self, index, value, role):
if index.isValid() and role == Qt.EditRole:
## col = index.column()
## row = self.rows[index.row()]
## row[col]=value
self.rows[index.row()][index.column()]=value
print self.rows[0]
self.dataChanged.emit(index, index)
return True
return False
def flags(self, index):
return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
def saveIt(self):
print self.rows[0]
with open(self.fileName, "wb") as fileOutput:
writer = csv.writer(fileOutput)
writer.writerow(self.header[0])
for rowNumber in range(len(self.rows)):
fields = self.rows[rowNumber]
writer.writerow(fields)
print ':)'
Attachment 10846
Re: Possible conversion from QStandardItemModel to QAbstractTableModel?
Suspicious of calling saveIt() in the AbstractModel via the QtableView nested button. I wonder if its causing the initial load-up of the CSV data to overwrite any editing just before save?
If this is the case, how can such be avoided?
Button call function in QtableView:
Code:
@QtCore.pyqtSlot()
def on_pushButtonSave_clicked(self):
CSVModel(self).saveIt()
__init__ for AbstractTableModel, and subsequent saveIt() function therein:
Code:
def __init__(self, fileName, parent=None):
super(CSVModel,self).__init__()
self.header = []
self.rows = []
self.fileName = r'E:\Documents\SIUC\2014\Fall\440 - Hydro\QGIS\test_bay\CSVtesting\mfLayer1_Grid.csv'
with open(self.fileName, "rb") as fileInput:
for idx, row in enumerate(csv.reader(fileInput)):
headerIDx = 0
if idx is headerIDx:
self.header.append(row)
elif idx>headerIDx:
items = [field for field in row]
self.rows.append(items)
self.rowsLoaded = CSVModel.activeRows
Code:
def saveIt(self):
with open(self.fileName, "wb") as fileOutput:
writer = csv.writer(fileOutput)
writer.writerow(self.header[0])
for rowNumber in range(len(self.rows)):
fields = self.rows[rowNumber]
writer.writerow(fields)
Re: Possible conversion from QStandardItemModel to QAbstractTableModel?
Could it be that you are creating a new model and call saveIt() on that instance instead of calling saveIt() in the model used by the view?
Cheers,
_