【问题标题】:'C++ object destroyed' in QComboBox descendant editor in delegate委托中的 QComboBox 后代编辑器中的“C++ 对象已销毁”
【发布时间】:2011-01-28 22:36:42
【问题描述】:

我修改了组合框来保存颜色,使用 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()
  • WindowBlocked
  • 油漆
  • 窗口停用
  • !!!!!!!!! “关闭编辑器”开火了!
  • 隐藏
  • HideToParent
  • 聚焦
  • 延迟删除
  • !!!!!!!!! #6e6eff

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

【问题讨论】:

    标签: pyqt qcombobox qitemdelegate


    【解决方案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。

    【讨论】:

      猜你喜欢
      • 2016-10-26
      • 2011-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-12
      • 1970-01-01
      相关资源
      最近更新 更多