4

In my Programme,

QLineEdit and QListWidget Placed in QVBoxLayout. And then QVBoxLayout put in a QFrame with Stylesheet of background-color:orange.

QLineEdit and QListWidget also get the same background-color of QFrame. How to avoid the background color overlapping ? .

Suppose, If we change the background color of QListwidget by style sheet, then scroll bar color also changed to QListWidget color.

How to avoid it?, I Need a native style layout ?

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()
Kumar
  • 592
  • 6
  • 18

1 Answers1

4

You have to set a selector (for example the objectName) in addition to indicating the class it will affect:

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

enter image description here

For more details I recommend reading the Qt docs:

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • hai @eyllanesc, Suppose if we call a style sheet from a function then, how we make it ? For example def func_Qframe_stylesheet_001(self): return """ QFrame{background-color: 'orange';} """ – Kumar May 27 '20 at 15:56
  • @Kumar I do not understand you – eyllanesc May 27 '20 at 15:58
  • @ eyllanesc, I make my stylesheet as a Function/method, and call it . Now, How Can i Over come this problem ? – Kumar May 27 '20 at 16:05
  • @Kumar It seems that you don't know much about python (I recommend checking your notes) since what you ask is trivial: change to `return "QFrame#frame{background-color:orange}"` and add `your_framesetObjectName("frame")` – eyllanesc May 27 '20 at 16:12
  • Yes, Mr. @eyllanesc, Absolutely you are correct. I am new to Python, Not only Python, I am new to Programme, With the help of Knowledgeable person, like You, I will Move Inch by Inch. Thanks a Lot. – Kumar May 27 '20 at 16:20
  • @Kumar Unfortunately SO is not a tutoring place so the next time you ask trivial questions I will avoid answering them. Bye – eyllanesc May 27 '20 at 16:24