开发者

'C++ object destroyed' in QComboBox descendant editor in delegate

开发者 https://www.devze.com 2022-12-23 13:55 出处:网络
I have modified combobox to hold colors, using QtColorCombo (http://qt.nokia.com/products/appdev/add-on-products/catalog/4/Widgets/qtcolorcombobox) as howto for the \'more...\' button implementation d

I have modified combobox to hold colors, using QtColorCombo (http://qt.nokia.com/products/appdev/add-on-products/catalog/4/Widgets/qtcolorcombobox) as howto for the 'more...' button implementation details. It works fine in C++ and in PyQt on linux, but I get 'underlying C++ object was destroyed' when use this control in PyQt on Windows. It seels like the error happens when:

...
# in constructor:
self.activated.connect(self._emitActivatedColor)
...
def _emitActivatedColor(self, index):
    if self._colorDialogEnabled and index == self.colorCount():
        print '!!!!!!!!! QtGui.QColorDialog.getColor()'
        c = QtGui.QColorDialog.getColor() # <----- :( delegate fires 'closeEditor'
        print '!!!!!!!!! ' + c.name()

        if c.isValid():
            self._numUserColors += 1
            #at the next line currentColor() tries to access C++ layer and fails
            self.addColor(c, self.currentColor(开发者_运维问答).name())

            self.setCurrentIndex(index)
...

Maybe console output will help. I've overridden event() in editor and got:

  • MouseButtonRelease
  • FocusOut
  • Leave
  • Paint
  • Enter
  • Leave
  • FocusIn
  • !!!!!!!!! QtGui.QColorDialog.getColor()
  • WindowBlocked
  • Paint
  • WindowDeactivate
  • !!!!!!!!! 'CloseEditor' fires!
  • Hide
  • HideToParent
  • FocUsOut
  • DeferredDelete
  • !!!!!!!!! #6e6eff

Can someone explain, why there is such a different behaviour in the different environments, and maybe give a workaround to fix this. Here is the minimal example: http://docs.google.com/Doc?docid=0Aa0otNVdbWrrZDdxYnF3NV80Y20yam1nZHM&hl=en


The problem seems to be a fact, that QColorDialog.color() shows modal dialog, that takes focus from combo, which closes immediately after that, then delegate destroys it.. ooops. So, the workaround to solve such problems is the event interruption:

In the delegate:

def eventFilter(self, editor, event):
    if event.type() == QtCore.QEvent.FocusOut and hasattr(editor, 'canFocusOut'):
        if not editor.canFocusOut: return False
    return QtGui.QItemDelegate.eventFilter(self, editor, event)

In editor we have to introduce the flag self.canFocusOut and set it to true when FocusOut is forbidden. I'm doing this when 'highlited' signal fires on the element, that shows QColorDialog.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号