如何摆脱两个小部件之间的空白

时间:2019-01-25 13:14:48

标签: pyqt qtstylesheets

我想对由标签和按钮组成的标题横幅进行编码。 除了标签和按钮之间仍然存在间隙之外,我几乎成功了。

a busy cat

您是否知道如何消除它?

我试图使用样式表一次在所有小部件上设置背景颜色。

我还尝试将其设置在容器QWidget#container上,但是它也不起作用。

也许可以在QPushButton上删除内置边距吗?

class HeaderBannerOneLineCloseButton(qt.QWidget):
    icon_path = resources.resource_path("icons/close-white.png")
    stylesheet = """
        * {
            background: %s;
            height: 56px;
        }

        QLabel#title {
            color: white;
            font-family: Ubuntu-Medium;
            font-size: 18px;
            padding-left: 31px;
            padding-right: 31px;
        }

        QPushButton#closeButton {
            background-origin: content;
            background-repeat: no-repeat;
            background-position: center middle;
            background-image: url("%s");
            border: none;
        }  
        """ % (colors.primary1, icon_path)

    def __init__(self, parent=None):
        super().__init__(parent)
        self.setObjectName("container")
        self.setStyleSheet(self.stylesheet)
        self.setSizePolicy(qt.QSizePolicy(qt.QSizePolicy.Expanding,
                                          qt.QSizePolicy.Fixed))

        self.titleLabel = qt.QLabel(self)
        self.titleLabel.setObjectName("title")
        self.titleLabel.setSizePolicy(qt.QSizePolicy(qt.QSizePolicy.Expanding,
                                                     qt.QSizePolicy.Expanding))

        self.closeButton = qt.QPushButton(self)
        self.closeButton.setObjectName("closeButton")

        layout = qt.QHBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.addWidget(self.titleLabel)
        layout.addWidget(self.closeButton)
        self.setLayout(layout)

    def setText(self, text):
        self.titleLabel.setText(text)

2 个答案:

答案 0 :(得分:1)

尝试一下:

from PyQt5 import QtWidgets as qt
from PyQt5 import QtGui     as gi

class HeaderBannerOneLineCloseButton(qt.QWidget):

#    icon_path = resources.resource_path("icons/close-white.png")

    stylesheet = """
        * {
            background: #2196f3;
            height: 56px;
        }

        QLabel#title {
            color: white;
            font-family: Ubuntu-Medium;
            font-size: 18px;
            padding-left: 31px;
            padding-right: 31px;
        }
/*
        QPushButton#closeButton {
            background-origin: content;
            background-repeat: no-repeat;
            background-position: center middle;
            background-image: url("D:/_Qt/img/close.png");   
            border: none;
        }  
*/        
/*  ++++++++++++++++++++++++++++++ */
        QToolButton{
            background:#2196f3;
            font-size:11px;
        }
        QToolButton:hover{
            background: #FF00FF;
            font-size:11px;        
        }
        """  # % (colors.primary1, icon_path)

    def __init__(self, parent=None):
        super().__init__(parent)
        self.setObjectName("container")
        self.setStyleSheet(self.stylesheet)
        self.setSizePolicy(qt.QSizePolicy(qt.QSizePolicy.Expanding,
                                          qt.QSizePolicy.Fixed))

        self.titleLabel = qt.QLabel("header banner made of a label and a button", self)
        self.titleLabel.setObjectName("title")
        self.titleLabel.setSizePolicy(qt.QSizePolicy(qt.QSizePolicy.Expanding,
                                                     qt.QSizePolicy.Expanding))

#        self.closeButton = qt.QPushButton("closeButton", self)
#        self.closeButton.setObjectName("closeButton")
        close = qt.QToolButton(self)                          # +++
        close.setIcon(gi.QIcon('D:/_Qt/img/close.png'))       # +++
        close.setMinimumHeight(10)                            # +++
        close.clicked.connect(self.close)                     # +++

        layout = qt.QHBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.addWidget(self.titleLabel)
#        layout.addWidget(self.closeButton)
        layout.addWidget(close)                               # +++

        self.setLayout(layout)

    def setText(self, text):
        self.titleLabel.setText(text)

if __name__ == "__main__":
    import sys
    app = qt.QApplication(sys.argv)
    win = HeaderBannerOneLineCloseButton()
    win.setFixedSize(450, 35)                  # +++
    win.show()
    sys.exit(app.exec_())

enter image description here

答案 1 :(得分:0)

对我的初始代码进行最少更改的简单解决方案是:

layout.setSpacing(0)

但是前面的答案很有道理。我应该使用工具按钮而不是按钮来具有图标。