then you do this in setEditorData() as in your example.
I am a bit confused of a small aspect of this. Via my example, the try:/except: block sets the index to zero... and while the initial table view displays in accordance with the index setting, the actual value is not emplaced in the data model - as discovered by simply saving the changes immediately after starting the table view (and checking the output file). IF a cell (comboBox) is clicked, then subsequently the unaltered / altered value is properly registered to the data model and observed in the file Output.
(hopefully that made sense).
It seems that another work-around is in order to get the default values to be properly inserted into the dataModel, even if the user does not make an initial selection of the cell (comboBox). My initial actions are warranted based on setModelData not initially being called, so possibly calling it 'manually' upon startup would be a solution? Yet, I am curious as to how the delegate sources the model information - as I get an error with the following (based on the model argument) mainly because I am a bit novice on how to instance the QAbstractTableModel in this circumstance:
def __init__(self, parent):
def createEditor(self, parent, option, index):
self.averageChoices = ['Arithmetic','Harmonic','Logarithmic']
averageCombo.addItems(self.averageChoices)
averageCombo.currentIndexChanged[int].connect(self.currentIndexChanged)
return averageCombo
def setEditorData(self, editor, index):
editor.blockSignals(True)
try: idxConversion = self.averageChoices.index(index.model().data(index,Qt.DisplayRole))
except ValueError: idxConversion = 0
editor.setCurrentIndex(int(idxConversion))
editor.blockSignals(False)
self.setModelData(self,editor,???,index) #<<============================?
def setModelData(self, editor, model, index):
model.setData(index, self.averageChoices[editor.currentIndex()],Qt.EditRole)
@QtCore.pyqtSlot(int)
def currentIndexChanged(self):
self.commitData.emit(self.sender())
class averageComboDelegate(QtGui.QItemDelegate):
def __init__(self, parent):
QtGui.QItemDelegate.__init__(self, parent)
def createEditor(self, parent, option, index):
self.averageChoices = ['Arithmetic','Harmonic','Logarithmic']
averageCombo = QtGui.QComboBox(parent)
averageCombo.addItems(self.averageChoices)
averageCombo.currentIndexChanged[int].connect(self.currentIndexChanged)
return averageCombo
def setEditorData(self, editor, index):
editor.blockSignals(True)
try: idxConversion = self.averageChoices.index(index.model().data(index,Qt.DisplayRole))
except ValueError: idxConversion = 0
editor.setCurrentIndex(int(idxConversion))
editor.blockSignals(False)
self.setModelData(self,editor,???,index) #<<============================?
def setModelData(self, editor, model, index):
model.setData(index, self.averageChoices[editor.currentIndex()],Qt.EditRole)
@QtCore.pyqtSlot(int)
def currentIndexChanged(self):
self.commitData.emit(self.sender())
To copy to clipboard, switch view to plain text mode
Added after 5 minutes:
Also:
When I turn the block off during 'priming' of the comoboBox - the signal to change data is not called, I suspect because this is occurring during the initialization of the widget? (frankly both these links could be omitted if this worked, but this could possibly create an infinite loop correct?)
...
averageCombo.currentIndexChanged[int].connect(self.currentIndexChanged)
return averageCombo
def setEditorData(self, editor, index):
editor.blockSignals(False) #<<=======================Was originally 'True'
try: idxConversion = self.averageChoices.index(index.model().data(index,Qt.DisplayRole))
except ValueError: idxConversion = 0
editor.setCurrentIndex(int(idxConversion))
editor.blockSignals(False)
def setModelData(self, editor, model, index):
model.setData(index, self.averageChoices[editor.currentIndex()],Qt.EditRole)
...
averageCombo.currentIndexChanged[int].connect(self.currentIndexChanged)
return averageCombo
def setEditorData(self, editor, index):
editor.blockSignals(False) #<<=======================Was originally 'True'
try: idxConversion = self.averageChoices.index(index.model().data(index,Qt.DisplayRole))
except ValueError: idxConversion = 0
editor.setCurrentIndex(int(idxConversion))
editor.blockSignals(False)
def setModelData(self, editor, model, index):
model.setData(index, self.averageChoices[editor.currentIndex()],Qt.EditRole)
To copy to clipboard, switch view to plain text mode
then you do this in setEditorData() as in your example.
I am a bit confused of a small aspect of this. Via my example, the try:/except: block sets the index to zero... and while the initial table view displays in accordance with the index setting, the actual value is not emplaced in the data model - as discovered by simply saving the changes immediately after starting the table view (and checking the output file). IF a cell (comboBox) is clicked, then subsequently the unaltered / altered value is properly registered to the data model and observed in the file Output.
(hopefully that made sense).
It seems that another work-around is in order to get the default values to be properly inserted into the dataModel, even if the user does not make an initial selection of the cell (comboBox). My initial actions are warranted based on setModelData not initially being called, so possibly calling it 'manually' upon startup would be a solution? Yet, I am curious as to how the delegate sources the model information - as I get an error with the following (based on the model argument) mainly because I am a bit novice on how to instance the QAbstractTableModel in this circumstance:
def __init__(self, parent):
def createEditor(self, parent, option, index):
self.averageChoices = ['Arithmetic','Harmonic','Logarithmic']
averageCombo.addItems(self.averageChoices)
averageCombo.currentIndexChanged[int].connect(self.currentIndexChanged)
return averageCombo
def setEditorData(self, editor, index):
editor.blockSignals(True)
try: idxConversion = self.averageChoices.index(index.model().data(index,Qt.DisplayRole))
except ValueError: idxConversion = 0
editor.setCurrentIndex(int(idxConversion))
editor.blockSignals(False)
self.setModelData(self,editor,???,index) #<<============================?
def setModelData(self, editor, model, index):
model.setData(index, self.averageChoices[editor.currentIndex()],Qt.EditRole)
@QtCore.pyqtSlot(int)
def currentIndexChanged(self):
self.commitData.emit(self.sender())
class averageComboDelegate(QtGui.QItemDelegate):
def __init__(self, parent):
QtGui.QItemDelegate.__init__(self, parent)
def createEditor(self, parent, option, index):
self.averageChoices = ['Arithmetic','Harmonic','Logarithmic']
averageCombo = QtGui.QComboBox(parent)
averageCombo.addItems(self.averageChoices)
averageCombo.currentIndexChanged[int].connect(self.currentIndexChanged)
return averageCombo
def setEditorData(self, editor, index):
editor.blockSignals(True)
try: idxConversion = self.averageChoices.index(index.model().data(index,Qt.DisplayRole))
except ValueError: idxConversion = 0
editor.setCurrentIndex(int(idxConversion))
editor.blockSignals(False)
self.setModelData(self,editor,???,index) #<<============================?
def setModelData(self, editor, model, index):
model.setData(index, self.averageChoices[editor.currentIndex()],Qt.EditRole)
@QtCore.pyqtSlot(int)
def currentIndexChanged(self):
self.commitData.emit(self.sender())
To copy to clipboard, switch view to plain text mode
Added after 39 minutes:
'Bleck...'
The code below is the solution I have settled upon (which isn't to kind on the eyes); and its the only one I have found to work (so far).
I suppose there might be an alternative, and if its discovered - I will update this posting for the probably miniscule list of people whom might be searching for a similar solution.
Its merely a function within the QAbstractTableModel which houses the default values for each particular column.
If the initlized values in the table (from source file) are found to be None-Type, then the defaults are inserted respectively.
def setDefaultCombos(self):
if 'Layer-Options' in self.editorType:
for row in range(len(self.rows)):
if self.rows[row][1] == '': \
self.
setData(self.
index(row,
1,QtCore.
QModelIndex()),
'True',Qt.
EditRole) if self.rows[row][2] == '': \
self.
setData(self.
index(row,
2,QtCore.
QModelIndex()),
'Confined',Qt.
EditRole) if self.rows[row][3] == '': \
self.
setData(self.
index(row,
3,QtCore.
QModelIndex()),
'Arithmetic',Qt.
EditRole) if self.rows[row][5] == '': \
self.
setData(self.
index(row,
5,QtCore.
QModelIndex()),
'Actual Value',Qt.
EditRole) if self.rows[row][6] == '': \
self.
setData(self.
index(row,
6,QtCore.
QModelIndex()),
'Active',Qt.
EditRole) if 'Stress Period-Options' in self.editorType:
for row in range(len(self.rows)):
if self.rows[row][4] == '': \
self.
setData(self.
index(row,
4,QtCore.
QModelIndex()),
'Steady State',Qt.
EditRole)
def setDefaultCombos(self):
if 'Layer-Options' in self.editorType:
for row in range(len(self.rows)):
if self.rows[row][1] == '': \
self.setData(self.index(row,1,QtCore.QModelIndex()),'True',Qt.EditRole)
if self.rows[row][2] == '': \
self.setData(self.index(row,2,QtCore.QModelIndex()),'Confined',Qt.EditRole)
if self.rows[row][3] == '': \
self.setData(self.index(row,3,QtCore.QModelIndex()),'Arithmetic',Qt.EditRole)
if self.rows[row][5] == '': \
self.setData(self.index(row,5,QtCore.QModelIndex()),'Actual Value',Qt.EditRole)
if self.rows[row][6] == '': \
self.setData(self.index(row,6,QtCore.QModelIndex()),'Active',Qt.EditRole)
if 'Stress Period-Options' in self.editorType:
for row in range(len(self.rows)):
if self.rows[row][4] == '': \
self.setData(self.index(row,4,QtCore.QModelIndex()),'Steady State',Qt.EditRole)
To copy to clipboard, switch view to plain text mode
Bookmarks