Results 1 to 2 of 2

Thread: Custom delegate calling setModelData regardless of editor accept / reject

  1. #1
    Join Date
    Oct 2009
    Posts
    2
    Qt products
    Platforms
    Unix/X11 Windows

    Default 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:
    Qt Code:
    1. def setModelData(self, editor, model, index):
    2. row = index.row()
    3. serial = editor.serialLineEdit.text().trimmed()
    4. barcode = editor.barcodeLineEdit.text().trimmed()
    5. barcode = None if not barcode else barcode
    6. responsable = editor.responsableComboBox.itemData(
    7. editor.responsableComboBox.currentIndex()).toInt()[0]
    8. agregado = editor.fechaAgregadoDateEdit.date()
    9. model.setData(model.index(row, pcols['SERIAL']), QVariant(serial))
    10. model.setData(model.index(row, pcols['BARCODE']), QVariant(barcode))
    11. model.setData(model.index(row, pcols['RESPONSABLE']),
    12. QVariant(responsable))
    13. model.setData(model.index(row, pcols['FECHA_AGREGADO']),
    14. QVariant(agregado))
    15. model.submitAll()
    To copy to clipboard, switch view to plain text mode 
    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.

  2. #2
    Join Date
    Oct 2009
    Posts
    2
    Qt products
    Platforms
    Unix/X11 Windows

    Default 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:

    Qt Code:
    1. from PyQt4.QtGui import QDialog
    2. import ui_portatilDlg
    3.  
    4. class editPortatilDialog(QDialog,
    5. ui_portatilDlg.Ui_Dialog):
    6.  
    7. def __init__(self, parent=None):
    8. super(editPortatilDialog, self).__init__(parent)
    9. self.setupUi(self)
    10.  
    11. def accept(self):
    12. self.setResult(QDialog.Accepted)
    13. QDialog.accept(self)
    14.  
    15. def cancel(self):
    16. self.setResult(QDialog.Rejected)
    17. QDialog.cancel(self)
    To copy to clipboard, switch view to plain text mode 

    And then modifiying setModelData in the custom delegate:

    Qt Code:
    1. def setModelData(self, editor, model, index):
    2. if editor.result() == QDialog.Accepted:
    3. row = index.row()
    4. binary = QByteArray()
    5. buff = QBuffer(binary)
    6. editor.fotoLabel.pixmap().save(buff, 'PNG')
    7. nombre = editor.nombreLineEdit.text().trimmed()
    8. apellido = editor.apellidoLineEdit.text().trimmed()
    9.  
    10. model.setData(model.index(row, ucols['NOMBRE']), QVariant(nombre))
    11. model.setData(model.index(row, ucols['APELLIDO']), QVariant(apellido))
    12. model.setData(model.index(row, ucols['FOTO']), QVariant(binary))
    13. model.submitAll()
    To copy to clipboard, switch view to plain text mode 

    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.

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.