【问题标题】:pyqt4 widget appear outside the layoutpyqt4 小部件出现在布局之外
【发布时间】:2020-06-02 04:48:18
【问题描述】:

我想要图片中的效果。 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()

【问题讨论】:

    标签: python pyqt pyqt4


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-15
      • 1970-01-01
      • 2019-02-21
      相关资源
      最近更新 更多