Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Custom QGraphicsLayout
Forum Updated to NodeBB v4.3 + New Features

Custom QGraphicsLayout

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 780 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    allohak
    wrote on last edited by
    #1

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

    @

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved