QFrame背景色与其他Widget如QLineEdit、QListBoxWidget等重叠。如何避免这种情况?

问题描述 投票:3回答:1

在我的程序中。

QLineEdit和QListWidget放置在QVBoxLayout中。然后将QVBoxLayout放入一个QFrame中,其Stylesheet为background-color:orange.QLineEdit和QListWidget也得到同样的QFrame背景色。

QLineEdit和QListWidget也得到同样的QFrame背景色。. 如何 避免背景色重叠 ? .

假设我们通过样式表改变了QListwidget的背景色,那么滚动条的颜色也变成了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()
python pyqt pyqt5 qframe
1个回答
3
投票

你必须设置一个选择器(例如objectName),此外还需要指明它将影响的类。

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

enter image description here

更多的细节我建议阅读Qt文档。

© www.soinside.com 2019 - 2024. All rights reserved.