我已经使用 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

有帮助吗?

解决方案

问题似乎是一个事实,即 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。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top