1

我修改了组合框来保存颜色,使用 QtColorCombo ( http://qt.nokia.com/products/appdev/add-on-products/catalog/4/Widgets/qtcolorcombobox ) 作为“更多...”按钮的操作方法实施细节。它在 C++ 和 Linux 上的 PyQt 中运行良好,但在 Windows 上的 PyQt 中使用此控件时,我得到“底层 C++ 对象被破坏”。似乎错误发生在以下情况:

...
# 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)
...

也许控制台输出会有所帮助。我在编辑器中覆盖了 event() 并得到:

  • 鼠标按钮释放
  • 焦点输出
  • 离开
  • 进入
  • 离开
  • 专注于
  • !!!!!!!!!QtGui.QColorDialog.getColor()
  • 窗口阻塞
  • 窗口停用
  • !!!!!!!!!“关闭编辑器”开火了!
  • 隐藏
  • 隐藏到父级
  • 聚焦
  • 延迟删除
  • !!!!!!!!!#6e6eff

有人可以解释一下,为什么在不同的环境中会有如此不同的行为,也许可以提供一种解决方法来解决这个问题。这是最小的例子: http ://docs.google.com/Doc?docid=0Aa0otNVdbWrrZDdxYnF3NV80Y20yam1nZHM&hl=en

4

1 回答 1

1

问题似乎是一个事实,QColorDialog.color() 显示模式对话框,它从组合中获取焦点,然后立即关闭,然后委托将其销毁.. 哎呀。因此,解决此类问题的解决方法是事件中断:

在代表中:

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)

在编辑器中我们必须引入标志 self.canFocusOut 并在 FocusOut 被禁止时将其设置为 true。当元素上触发'highlited'信号时,我正在执行此操作,显示QColorDialog。

于 2010-03-18T13:48:51.967 回答