pyqt4 小部件出现在布局之外

pyqt4 widget appear outside the layout

我要图中的效果。 qlabel在布局中,字体居中。下面是我写的代码,和我想要的不一样。一是qlabel在布局之外,二是字体没有居中。

import sys

from PyQt4.QtCore import Qt
from PyQt4.QtGui import QLabel, QApplication, QWidget, QVBoxLayout, QStyleOption, QPainter, QStyle


class Example(QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.setGeometry(100, 100, 500, 500)

        vbox = QVBoxLayout()
        self.setLayout(vbox)

        a = QLabel('aaa')
        b = QLabel('bbb')
        vbox.addWidget(a)
        vbox.addWidget(a, alignment=Qt.AlignHCenter | Qt.AlignTop)
        vbox.addWidget(b, alignment=Qt.AlignHCenter | Qt.AlignBottom)

        self.setStyleSheet("""
                Example{
             border-width: 1px;
             border-style: solid;
             border-color: red;
                min-width:500px;
                 max-width:500px;
                 min-height:500px;
                 max-height:500px;
                 padding:1px 1px 1px 1px;
                 margin-bottom:30px;
                }
            QLabel{
                min-width:500px;
                 max-width:500px;
                 min-height:50px;
                 max-height:50px;
                 margin:0px;
                 padding:0px;
                 background-color:#cdcdcd;
                 text-align: center center;
            }
        """)
        self.show()

    def paintEvent(self, event):
        super(Example, self).paintEvent(event)
        opt = QStyleOption()
        opt.initFrom(self)
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)
        self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self)


def main():
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

布局的逻辑是处理元素的几何形状,但您也在使用样式表处理几何形状,导致您观察到的问题。另一方面,QLabel 没有文本对齐 属性 但你必须使用 qproperty-alignment: AlignCenter;.

为避免这种情况,最好避免使用样式表管理几何图形,而是使用 class 的方法,考虑到解决方案是:

import sys

from PyQt4.QtCore import Qt
from PyQt4.QtGui import (
    QLabel,
    QApplication,
    QWidget,
    QVBoxLayout,
    QStyleOption,
    QPainter,
    QStyle,
)


class Example(QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.setGeometry(100, 100, 500, 500)

        self.label_a = QLabel("aaa")
        self.label_b = QLabel("bbb")
        self.label_a.setFixedHeight(50)
        self.label_b.setFixedHeight(50)

        vbox = QVBoxLayout(self)
        vbox.setContentsMargins(1, 1, 1, 1)
        vbox.addWidget(self.label_a)
        vbox.addWidget(QWidget(), stretch=1)
        vbox.addWidget(self.label_b)

        self.setFixedSize(500, 500)

        self.setStyleSheet(
            """
        Example{
            border-width: 1px;
            border-style: solid;
            border-color: red;
            padding:1px 1px 1px 1px;
        }
        QLabel{
            margin:0px;
            padding:0px;
            background-color:#cdcdcd;
            qproperty-alignment: AlignCenter;
        }
        """
        )

    def paintEvent(self, event):
        super(Example, self).paintEvent(event)
        opt = QStyleOption()
        opt.initFrom(self)
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)
        self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self)


def main():
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()