QFrame Background Color 和QLineEdit, QListBoxWidget 等其他Widgets 重叠了。如何避免?

QFrame Background Color overlapped with other Widgets like QLineEdit, QListBoxWidget, etc. How to avoid it?

在我的节目中,

QLineEdit和QListWidget放在QVBoxLayout中。然后 QVBoxLayout 放入一个 Stylesheet 为 background-color:orange 的 QFrame。

QLineEdit 和 QListWidget 也获得与 QFrame 相同的背景色。如何避免背景颜色重叠? .

假设,如果我们通过样式改变QListwidget的背景色sheet,那么滚动条的颜色也变成了QListWidget的颜色。

如何避免?,我需要原生样式布局?

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

item = ["Python", "Python 2.7", "Python 2.9", "Python 3.5", "Python 3.7", "National", "Zebra",
                "Apple", "X Ray","Boat", "Tiger", "Item001", "Item002", "Item003", "Item004", "Item005",
                "001Item", "002Item", "003Item","004Item", "005Item", "Ball", "Cat", "Dog", "Fish",
                "Gold Fish", "Star Fish"]


class myList(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Frame Example")
        self.myui()

    def myui(self):

        self.textbox = QLineEdit(self)
        self.listbox = QListWidget(self)
        self.listbox.addItems(item)

        vbox = QVBoxLayout()
        vbox.addWidget(self.textbox)
        vbox.addWidget(self.listbox)

        frame = QFrame()
        frame.setLayout(vbox)
        frame.setStyleSheet("background-color:orange")

        main_layout =QHBoxLayout()
        main_layout.addWidget(frame)
        self.setLayout(main_layout)

def main():
    myapp = QApplication(sys.argv)
    mywin = myList()
    mywin.show()
    sys.exit(myapp.exec_())

if __name__ == '__main__':
    main()

你必须设置一个选择器(例如 objectName)除了指明 class 它会影响:

frame = QFrame()
frame.setObjectName("frame")
frame.setLayout(vbox)
frame.setStyleSheet("QFrame#frame{background-color:orange}")

有关更多详细信息,我建议阅读 Qt 文档: