布局未扩展到整个窗口

时间:2018-08-25 19:20:40

标签: python layout pyqt5

我正在编写一个简单的加载屏幕,以备日后使用。我已经有一段时间没有使用PyQt了,现在一切都伴随着尝试和失败。我想要一个QProgressBar,它几乎扩展到边界的尽头。但是我得到了这个:

You can see the very thin progressbar not reaching the right border of the layout.

这让我认为是布局没有扩大。希望您的帮助和经验!谢谢!

我的代码:

class LoadingScren(QWidget):

    def __init__(self):
        super().__init__()

        self.windowWidth = 500
        self.windowHeight = 300
        self.backgroundImage = 'bg.jpg'
        self.icon = 'template_icon.png'
        self.iconWidht = 100
        self.iconHeight = 100
        self.headline = 'Headline'
        self.description = """Lorem ipsum dolor sit amet"""

        self.initUI()

    def initUI(self):
        self.setFixedSize(self.windowWidth, self.windowHeight)
        self.setWindowFlag(Qt.FramelessWindowHint)

        imgIcon = QLabel()
        imgIcon.setPixmap(QPixmap(self.icon).scaled(self.iconWidht, self.iconHeight))

        lblHeadline = QLabel()
        lblHeadline.setText(self.headline)
        lblHeadline.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        lblHeadline.setStyleSheet('font-size: 75px')

        lblDescription = QLabel()
        lblDescription.setScaledContents(True)
        lblDescription.setText(self.description)
        lblDescription.setStyleSheet('font-size: 25px')

        progressBar = QProgressBar()
        progressBar.setMaximumHeight(2)
        progressBar.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)

        headerLayout = QHBoxLayout()
        headerLayout.addWidget(imgIcon)
        headerLayout.addWidget(lblHeadline)

        mainLayout = QVBoxLayout()
        mainLayout.addLayout(headerLayout)
        mainLayout.addWidget(lblDescription)
        mainLayout.addItem(QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding))
        mainLayout.addWidget(progressBar)

        self.setLayout(mainLayout)
        self.show()

1 个答案:

答案 0 :(得分:0)

使用progressBar.setTextVisible(False)

Qt样式表progressBar.setStyleSheet(...)

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui     import *
from PyQt5.QtCore    import *

class LoadingScren(QWidget):

    def __init__(self):
        super().__init__()

        self.windowWidth  = 500
        self.windowHeight = 300
        #self.backgroundImage = 'bg.jpg'            
        self.icon = 'qt-logo.png'            #'template_icon.png'
        self.iconWidht  = 100
        self.iconHeight = 100
        self.headline = 'Headline'
        self.description = """Lorem ipsum dolor sit amet"""

        self.initUI()

    def initUI(self):
        self.setFixedSize(self.windowWidth, self.windowHeight)
        self.setWindowFlag(Qt.FramelessWindowHint)

        imgIcon = QLabel()
        imgIcon.setPixmap(QPixmap(self.icon).scaled(self.iconWidht, self.iconHeight))

        lblHeadline = QLabel()
        lblHeadline.setText(self.headline)
        lblHeadline.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        lblHeadline.setStyleSheet('font-size: 75px')

        lblDescription = QLabel()
        lblDescription.setScaledContents(True)
        lblDescription.setText(self.description)
        lblDescription.setStyleSheet('font-size: 25px')

        progressBar = QProgressBar()
        progressBar.setMaximumHeight(2)
        progressBar.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)

        progressBar.setTextVisible(False)
        # or
        #progressBar.setStyleSheet("""QProgressBar {
        #                              border: 1px solid red;
        #                         }""")

        headerLayout = QHBoxLayout()
        headerLayout.addWidget(imgIcon)
        headerLayout.addWidget(lblHeadline)

        mainLayout = QVBoxLayout()
        mainLayout.addLayout(headerLayout)
        mainLayout.addWidget(lblDescription)
        mainLayout.addItem(QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding))
        mainLayout.addWidget(progressBar)

        self.setLayout(mainLayout)
        self.show()

if __name__ == '__main__':
    app     = QApplication(sys.argv)
    mainWnd = LoadingScren()
    sys.exit(app.exec_())

enter image description here

enter image description here