views:

108

answers:

1

Hi, all! 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&amp;hl=en

+1  A: 

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.

Max