繁体   English   中英

更改 QString 或 QLineEdit 的颜色和字体

[英]Change the Color and Font of QString or QLineEdit

如何更改 QLineEdit 的颜色和字体?

这是我的代码:

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

文档中的setText行说里面的文本是 QString 我怎样才能改变它的字体和颜色?

对于颜色,请使用QPallete ,然后使用{your palette}.setColor(QtGui.QPalette.Text, {your QColor}) ,字体使用QFont

我的解决方案:

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_())

在此处输入图片说明

您可以使用以下方法更改颜色:

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

字体大小:

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

在 pyqt5 中,您也可以通过以下方式使用 StyleSheet 来完成:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM