'Change the Color and Font of QString or QLineEdit

How can I change the color and font of QLineEdit?

Here is my code:

self.lineEdit = QtGui.QLineEdit(widget)
self.lineEdit.setText("enter keywords here") #I want this to be in italics and in brown color

The setText line from Documentation says the text inside is of QString how can I change it's font and color?



Solution 1:[1]

You can change the color with:

self.lineEdit.setStyleSheet("color: rgb(x,x,x)")

Font size with:

self.lineEdit.setStyleSheet("fontName='Times-Italic'")

Solution 2:[2]

in pyqt5 also you can do it with StyleSheet in this way:

qrc = """
 /* Main LineEdit Setting */
 QLineEdit{
    background-color: #18181d;
    color: red;
    border-radius: 20%;
    height: 2.6em;

    font-weight: bold;
    font-family: 'Times New Roman';
 }

 /* when in hover */
 QLineEdit:hover{
    background-color: #25252d;
 }

 /* when has focus */
 QLineEdit:focus{
    border: 2px solid #13c386; 
    background-color: #25252d;
 }
"""
self.lineEdit.setStyleSheet(qrc)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ram karishna reddy vaddula
Solution 2 Qasim