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. QGraphicsLayout - Unexpected Extra Spacing Between Items
Qt 6.11 is out! See what's new in the release blog

QGraphicsLayout - Unexpected Extra Spacing Between Items

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 908 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.
  • B Offline
    B Offline
    biergaizi
    wrote on last edited by
    #1

    I am using QGraphics Framework to write some prototypes. I got in trouble on simulating regular layouting. There is a large spacing between these two widgets, but I set all the possible spacing to 0 already.

    !http://blog001.oss.aliyuncs.com/white_space.png(White Space Betweet the Image and the Text)!

    Here is my code (Sorry, it should be shorter):

    @
    import sys
    from PyQt4 import QtCore, QtGui
    from PyQt4.QtCore import Qt

    class PixmapLayoutItem(QtGui.QGraphicsLayoutItem):

    def __init__(self, image, parent=None):
        super(PixmapLayoutItem, self).__init__(parent)
        self.pixmapItem = QtGui.QGraphicsPixmapItem()
        self.pixmapItem.setPixmap(QtGui.QPixmap(image))
        self.setGraphicsItem(self.pixmapItem)
        print(self.pixmapItem.boundingRect().size(),
              self.pixmapItem.pixmap().size())
    
    def sizeHint(self, which, constraint=QtCore.QSizeF()):
        return self.pixmapItem.boundingRect().size()
    
    def setGeometry(self, rect):
        self.pixmapItem.setPos(rect.topLeft())
    

    class TextLayoutItem(QtGui.QGraphicsLayoutItem):

    def __init__(self, text, parent=None):
        super(TextLayoutItem, self).__init__(parent)
        self.textItem = QtGui.QGraphicsTextItem()
        self.textItem.setHtml(text)
        self.setGraphicsItem(self.textItem)
        print(self.textItem.boundingRect().size())
    
    def sizeHint(self, which, constraint=QtCore.QSizeF()):
        return self.textItem.boundingRect().size()
    
    def setGeometry(self, rect):
        self.textItem.setPos(rect.topLeft())
    

    class MainWindow(QtGui.QGraphicsView):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
    
        mainLayout = QtGui.QGraphicsLinearLayout()
        mainLayout.setSpacing(0)
    
        avatar = self._avatar()
        mainLayout.addItem(avatar)
        mainLayout.setAlignment(avatar, Qt.AlignCenter)
    
        text = self._text()
        mainLayout.addItem(text)
        mainLayout.setAlignment(text, Qt.AlignCenter)
    
        mainWidget = QtGui.QGraphicsWidget()
        mainWidget.setLayout(mainLayout)
    
        scene = QtGui.QGraphicsScene()
        scene.addItem(mainWidget)
        self.setScene(scene)
    
    @staticmethod
    def _avatar():
        pixmap = PixmapLayoutItem("./avatar.jpg")
        text = TextLayoutItem("Hello, world!")
    
        avatarLayout = QtGui.QGraphicsLinearLayout()
        avatarLayout.setOrientation(Qt.Vertical)
        avatarLayout.setSpacing(0)
    
        avatarLayout.addItem(pixmap)
        avatarLayout.setAlignment(pixmap, Qt.AlignCenter)
    
        avatarLayout.addItem(text)
        avatarLayout.setAlignment(text, Qt.AlignCenter)
    
        avatarWidget = QtGui.QGraphicsWidget()
        avatarWidget.setWindowFrameMargins(0, 0, 0, 0)
        avatarWidget.setContentsMargins(0, 0, 0, 0)
        avatarWidget.setLayout(avatarLayout)
    
        return avatarWidget
    
    @staticmethod
    def _text():
        text = TextLayoutItem("UNIX - Where is a shell, where is a way.")
        return text
    

    if name == "main":
    App = QtGui.QApplication(sys.argv)
    main = MainWindow()
    main.show()
    App.exec()
    @

    I don't know what is the root of the issue.

    Thanks.

    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