Custom QGraphicsLayout



  • I wrote a custom QGraphicsLayout, but the size of child QGraphicsWidgets that have been added to the layout do not seems to effect the size of the parent QGraphicsWidget that is using the QGraphicsLayout.

    If I constrain size of the parent QGraphicsWidget and tell the child QGraphicsWidgets that I place inside the layout to have expanding size policies, those children will correctly take the size of the parent. However, if I do not constrain the size of the parent but do set a preferred size policy for the children inside the layout, I would expect the layout to update its own size. But this is not the case.

    I believe it has something to do with not correctly handling event's received inside widgetEvent, but I'm not sure about this. Has anyone implemented their own custom layout before and know how to accomplish this? I get the behavior I am expecting when using QGraphicsLinearLayout, so I know it can be done.

    Thanks!

    I'm using PySide (Python), but I'm asking in general because I don't think this is a language specific question. I'm including the code below to show what things I am currently doing in my layout.
    @
    class OverlayLayout(QGraphicsLayout):
    def init(self,fill=False):
    super(OverlayLayout,self).init()
    self.items = list()
    self.fill = fill
    self.setGraphicsItem(self.parentLayoutItem())
    self.setContentsMargins(0,0,0,0)
    self.setSizePolicy( QSizePolicy(QSizePolicy.MinimumExpanding,QSizePolicy.MinimumExpanding))

    def sizeHint(self,which,constraint):
        w = 0
        h = 0
        for i in self.items:
            size = i.sizeHint(which,constraint)
            w=max(w,size.width())
            h=max(h,size.height())
        return QSizeF(w,h)
    
    def setGeometry(self,rect):
    

    super(OverlayLayout,self).setGeometry(rect)

        for item in self.items:
            if self.fill:
                item.setGeometry(rect)
            else:
                if item.sizePolicy().verticalPolicy() == QSizePolicy.MinimumExpanding and item.sizePolicy().horizontalPolicy() == QSizePolicy.MinimumExpanding:
                    item.setGeometry(rect)
                elif item.sizePolicy().verticalPolicy() == QSizePolicy.MinimumExpanding:
                    item.setGeometry(QRect(item.x(),item.y(),item.minimumWidth(),rect.height()))
                elif item.sizePolicy().horizontalPolicy() == QSizePolicy.MinimumExpanding:
                    item.setGeometry(QRect(item.x(),item.y(),rect.width(),item.minimumHeight()))
                else:
                    item.setGeometry(QRect(item.x(),item.y(),item.width,item.height))
           
    def addItem(self,item):
        self.items.append(item)
        self.addChildLayoutItem(item)
        
    def count(self):
        return len(self.items)
    
    def itemAt(self,i):
        return self.items[i]
    
    def removeAt(self,i):
        self.items[i].setParentLayoutItem(None)
        self.items.pop(i)
    
    def invalidate(self):
        QCoreApplication.postEvent(self.parentLayoutItem(),QEvent(QEvent.LayoutRequest))
    

    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.