PDA

View Full Version : Custom delegate calling setModelData regardless of editor accept / reject



wasert
26th October 2009, 15:43
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:



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']),
QVariant(responsable))
model.setData(model.index(row, pcols['FECHA_AGREGADO']),
QVariant(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.

wasert
28th October 2009, 19:42
I have managed to get around this issue, by implementing the following methods in the custom dialog used by the editor:



from PyQt4.QtGui import QDialog
import ui_portatilDlg

class editPortatilDialog(QDialog,
ui_portatilDlg.Ui_Dialog):

def __init__(self, parent=None):
super(editPortatilDialog, self).__init__(parent)
self.setupUi(self)

def accept(self):
self.setResult(QDialog.Accepted)
QDialog.accept(self)

def cancel(self):
self.setResult(QDialog.Rejected)
QDialog.cancel(self)


And then modifiying setModelData in the custom delegate:



def setModelData(self, editor, model, index):
if editor.result() == QDialog.Accepted:
row = index.row()
binary = QByteArray()
buff = QBuffer(binary)
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.