Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QGraphicsProxyWidget undo ItemStacksBehindParent?



  • Hello,
    I'm running into a problem with items / widgets stacking.

    I've:
    QGraphicsItem
    QGraphicsProxyWidget (parented under the QGraphicsItem)
    QTextEdit in the QGraphicsProxyWidget

    By default the QGraphicsProxyWidget has the ItemStacksBehindParent flag so that I cannot see nor interact with the QTextEdit, when double clicking on my QGraphicsItem, I remove the ItemStacksBehindParent flag so that my QTextEdits becomes visible and editable.

    I can see it and it has focus but I cannot type anything in it...

    If I remove the ItemStacksBehindParent flag , I can edit the QTextEdit properly but then I lose my QGraphicsItem behavior... and I need that behavior.

    Any idea why the QTextEdit doesn't work with the flag? I'm sure I'm forgetting something stupid.

    class GraphicsItem(QGraphicsItem):
        def __init__(self):
            super(GraphicsItem, self).__init__()
    
            self.browser = TextBrowser()
            self.editor = TextEdit()
            self.proxy = QtWidgets.QGraphicsProxyWidget(self)
            self.proxy.setWidget(self.browser)
            self.proxy.setFlag(QtWidgets.QGraphicsItem.ItemStacksBehindParent)
            self.displayedWidget = self.browser
    
        def mouseDoubleClickEvent(self, event):
            self.proxy.setFlags(self.flags() & ~self.ItemStacksBehindParent)
            self._swapWidget()
            super(GraphicsItem, self).mouseDoubleClickEvent(event)
    
        def _swapWidget(self):
            if isinstance(self.displayedWidget, QtWidgets.QTextBrowser):
                self.proxy.setWidget(self.editor)
                self.displayedWidget = self.editor
                self.editor.setFocus()
    
            else:
                self.proxy.setWidget(self.browser)
                self.displayedWidget = self.browser
                self.browser.setFocus()
                self.proxy.setFlag(QtWidgets.QGraphicsItem.ItemStacksBehindParent)
    
    

    Thanks!