Pysyde/QTableView: openPersistentEditor works only once?



  • Hi guys,
    I have a table view that has to display some comboboxes on its 5th column.
    So, on init, i set a delegate (previewModeDelegate) and draw some comboboxes:

    def initComboBoxes(self):
    
        model = self.view.model()
        self.view.setItemDelegateForColumn(4,  previewModeDelegate(self))
    
        for r in range(model.rowCount()):
            res = self.view.openPersistentEditor(model.index(r, 4))
    

    the delegate:

    class previewModeDelegate(QItemDelegate):
    
        def __init__(self, parent):
            QItemDelegate.__init__(self, parent)
            self.items = []
            self.items.append("No FX")
            self.items.append("Motion Blur")
            self.items.append("DOF")
            self.parent = parent
    
        def createEditor(self, parent, option, index):
    
            self.editor = QComboBox(parent)
    
            for i in range(len(self.items)):
                self.editor.addItem(self.items[i])
    
            # getting node, getting the data 
            cNode = index.model().cameraData[index.row()]
            value = cNode.GetProperty("previewMode")
    
            self.editor.connect(self.editor, SIGNAL("currentIndexChanged(int)"), self.cbCallback)
            self.editor.setCurrentIndex(value)
    
            return self.editor
    
        def cbCallback(self, value):
    
            editor = self.sender()
            self.parent.view.commitData(editor)
            self.parent.view.closeEditor(editor, QAbstractItemDelegate.NoHint)
    
        def setModelData(self, editor, model, index):
    
            value = editor.currentIndex()
            
            # getting the node, setting the data
            cNode = model.cameraData[index.row()]
            cNode.SetProperty("previewMode", value)
    
            return True
    

    everything works fine, i get some nice working comboboxes:

    0_1558020728670_6312abcc-746c-47cd-b8a0-6e00f43492c3-image.png

    problem is. during the sw usage, i have to reset the model and recreate the comboboxes.
    So, in the gui init, i connected the InitComboBoxes method to the the resetModel signal

    self.cameraModel.modelReset.connect(self.initComboBoxes)
    

    After i reset the model, the initComboBoxes method is launched but the comboboxes are not reinitialized correctly:

    0_1558020990731_8f1c6856-d5fa-4c50-a99c-5d11e71f0c04-image.png

    I am still very new to Pyside MVC. I tried tinkering with the code, but with no results. Any help?

    Ty,
    Federico


  • Lifetime Qt Champion

    Hi,

    Did you check the size of the model when initComboBoxes is called ?
    If it's valid, can you try not replacing the delegate ?



  • @dammilo
    I use PyQt, but pretty similar to PySide. This probably has nothing to do with your problem, but...

    self.parent = parent
    

    QItemDelegate inherits all the way from QObject.parent() method. Is it a good idea to create an instance attribute with the same name as an inherited method?

    In your code, you could get rid of that anyway and simply use the existing parent() method instead. You won't need your own parent attribute in any of your classes deriving from QObject.



  • thank you guys for your inputs

    @SGaist
    hi, i checked and the model looks always valid to me.
    My impression is that on a model level everything works - in fact, the numbers that i see on the combobox column are coming straight out of it.
    Also, I just noticed that if i double click the cell and go into edit mode, I still get my combobox delegate and I can correctly edit the values; the combobox disappears as soon as i'm done with the edit.

    I tried moving the setItemDelegateForColumn method around - only in init, after the openPersistentEditor, before it, duplicated...but no luck

    @JonB
    good call, didn't know that, I fixed it. But sadly, as you said, that didn't solve the issue



  • Breakthrough!

    If I connect the rebuild function to a custom signal that I emit just after the endResetModel(), everything works.

    Why is that?
    Maybe the modelReset signal is not emitted when I expect it to?


  • Lifetime Qt Champion

    modelReset should be emit after endResetMode.

    What version of PySide are you using ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.