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

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

Answers


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.


Need Your Help

How to create new workbook(blank) in excel and prompt user for name and file location

excel vba excel-vba

Suppose I want a create a new excel workbook(blank) using macros and the user should be prompted to enter the file name and location, the new file should not overwrite some existing file with the s...

How to do a JSON encoded GET request with AFNetworking-2.0?

ios json rest get afnetworking-2

I'm trying to use a pattern similar to this post which describes doing a PUT/POST for RESTful API, but for a GET.