简体   繁体   English

PyQt4-在继承自QWidget的对象上应用样式表

[英]PyQt4 - Applying a stylesheet on an object which inherits from QWidget

I am having an issue with PyQt4. 我在使用PyQt4时遇到问题。 I have a class which inherits from QWidget. 我有一个继承自QWidget的类。 This class uses a layout to store a QLabel and a QLineEdit. 此类使用布局存储QLabel和QLineEdit。 Here is the code: 这是代码:

class SearchBar(QtGui.QWidget):
    def __init__(self, parent=None):
        super(SearchBar, self).__init__(parent)
        self.setStyleSheet(SEARCHBAR_STYLE)

        layout = QtGui.QHBoxLayout()
        layout.setSpacing(0)
        layout.setMargin(0)
        layout.addStrut(SEARCHAR_HEIGHT)

        lbl_notification = QtGui.QLabel('Hi')
        lbl_notification.setStyleSheet(SEARCHBAR_NOTIFICATION_STYLE)
        layout.addSpacing(10)
        layout.addWidget(lbl_notification)

        searchbox = QLineEdit('Search')
        layout.addStretch()
        layout.addWidget(searchbox)
        layout.addSpacing(10)

        self.setLayout(layout)

and here is the stylesheet: 这是样式表:

SEARCHBAR_STYLE = """
                  QWidget {
                      background: #424a7d;
                  }
                  .QWidget {
                      border: 1px solid grey;
                  }
                  QLabel {
                      border-top: 1px solid grey;
                      border-bottom: 1px solid grey;
                  }
                  """

Now, my problem is that the stylesheet does not apply the way I would like it to. 现在,我的问题是样式表无法应用我想要的方式。 It only applies on my QLabel when the border should be around the whole object: 仅当边框应围绕整个对象时,它才适用于我的QLabel:

在此处输入图片说明

When I had a function creating my search bar as a QWidget, it worked perfectly, but now that I changed that to a class it does not work. 当我有一个将搜索栏创建为QWidget的函数时,它可以正常工作,但是现在我将其更改为一个类,因此它不起作用。 What am I doing wrong? 我究竟做错了什么?

EDIT: I am trying to achieve this: 编辑:我正在努力实现这一目标:

在此处输入图片说明

EDIT 2: The previous code, before I change it to a class, was this: 编辑2:在我将其更改为类之前,以前的代码是这样的:

def create_bar():
    layout = QtGui.QHBoxLayout()
    layout.setSpacing(0)
    layout.setMargin(0)
    layout.addStrut(SEARCHAR_HEIGHT)

    lbl_notification = QtGui.QLabel('Hi')
    lbl_notification.setStyleSheet(SEARCHBAR_NOTIFICATION_STYLE)
    layout.addSpacing(10)
    layout.addWidget(lbl_notification)

    search_bar = QtGui.QLineEdit('Search')
    search_bar.setMinimumSize(200, 25)
    search_bar.setMaximumSize(200, 25)
    search_bar.setStyleSheet(SEARCHBOX_STYLE)

    layout.addStretch()
    layout.addWidget(search_bar)
    layout.addSpacing(10)

    widget = QtGui.QWidget()
    widget.setStyleSheet(SEARCHBAR_STYLE)
    widget.setLayout(layout)
    return widget

Change the base class of SearchBar from QWidget to QFrame, or alternatively implement a style sheet aware paintEvent : 将SearchBar的基类从QWidget更改为QFrame,或者实现样式表感知的paintEvent

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

Then change the style sheet to 然后将样式表更改为

SEARCHBAR_STYLE = """
SearchBar {
    background: #424a7d;
    border: 1px solid grey;
}
"""

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM