2

我正在使用QGraphics Framework编写一些原型。我在模拟常规布局时遇到了麻烦。

这两个小部件之间的间距很大,但我已经将所有可能的间距设置为 0。需要注意的是,我正在使用Awesome Windows Manager,以便窗口周围没有任何边框。

带有多余空间的简单原型

这是我的代码(对不起,它应该更短):

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

我不知道问题的根源是什么。

谢谢。

4

1 回答 1

2

更改_avatar函数中的行:

avatarWidget.setContentsMargins(0, 0, 0, 0)

avatarLayout.setContentsMargins(0, 0, 0, 0)

间距将从这里开始: 这

对此:

这

不知道这对你来说是否足够小,但它失去了大部分的差距。

更新

使用较小的像素图,您会看到图像和 UNIX 文本之间存在间隙,因为“Hello world!” 文本比像素图更宽(我用黑色突出显示了头像小部件边框):

使文本更短将使头像小部件更小并失去明显的差距:

于 2013-07-17T09:53:17.927 回答