Custom delegate calling setModelData regardless of editor accept / reject
Hello,
I am using a custom delegate to provide a QDialog window as an editor to update my entire model at once.
The dialog is displayed properly and the initial editor data is being setup correctly as well.
The model data is saved fine when I click the accept button. However, when I change the values in the editor and click the cancel button, which is connected to the dialogs's reject() signal, the model data is updated too.(that is, the setModelData method of my custom delegate is called)
Here is the method:
Code:
def setModelData(self, editor, model, index):
row = index.row()
serial = editor.serialLineEdit.text().trimmed()
barcode = editor.barcodeLineEdit.text().trimmed()
barcode = None if not barcode else barcode
responsable = editor.responsableComboBox.itemData(
editor.responsableComboBox.currentIndex()).toInt()[0]
agregado = editor.fechaAgregadoDateEdit.date()
model.
setData(model.
index(row, pcols
['SERIAL']),
QVariant(serial
)) model.
setData(model.
index(row, pcols
['BARCODE']),
QVariant(barcode
)) model.setData(model.index(row, pcols['RESPONSABLE']),
model.setData(model.index(row, pcols['FECHA_AGREGADO']),
model.submitAll()
I have set the edit strategy to manual submit by calling:
model.setEditStrategy(model.OnManualSubmit)
I really have no idea of how to fix this. Any help is greatly appreciated.
Re: Custom delegate calling setModelData regardless of editor accept / reject
I have managed to get around this issue, by implementing the following methods in the custom dialog used by the editor:
Code:
import ui_portatilDlg
ui_portatilDlg.Ui_Dialog):
def __init__(self, parent=None):
super(editPortatilDialog, self).__init__(parent)
self.setupUi(self)
def accept(self):
def cancel(self):
And then modifiying setModelData in the custom delegate:
Code:
def setModelData(self, editor, model, index):
if editor.
result() == QDialog.
Accepted: row = index.row()
editor.fotoLabel.pixmap().save(buff, 'PNG')
nombre = editor.nombreLineEdit.text().trimmed()
apellido = editor.apellidoLineEdit.text().trimmed()
model.
setData(model.
index(row, ucols
['NOMBRE']),
QVariant(nombre
)) model.
setData(model.
index(row, ucols
['APELLIDO']),
QVariant(apellido
)) model.
setData(model.
index(row, ucols
['FOTO']),
QVariant(binary
)) model.submitAll()
However, I think this is sort of a hack, and that the accept() and cancel() methods should not be overriden, as the correct behavior should be inherited from QDialog.