Change the Color and Font of QString or QLineEdit

25.8k views Asked by At

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?

3

There are 3 answers

2
eyllanesc On BEST ANSWER

For the Color use QPallete, then use {your palette}.setColor(QtGui.QPalette.Text, {your QColor}), and the font use QFont

My Solution:

from PyQt4 import QtGui

from PyQt4 import QtCore


if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    w = QtGui.QLineEdit()
    palette = QtGui.QPalette()
    palette.setColor(QtGui.QPalette.Text, QtCore.Qt.red)
    w.setPalette(palette)
    font = QtGui.QFont("Times", 15, QtGui.QFont.Bold)
    w.setFont(font)
    w.show()
    sys.exit(app.exec_())

enter image description here

1
ram karishna reddy vaddula On

You can change the color with:

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

Font size with:

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

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)