【问题标题】:Change the Color and Font of QString or QLineEdit更改 QString 或 QLineEdit 的颜色和字体
【发布时间】:2017-05-14 05:50:45
【问题描述】:

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

这是我的代码:

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

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

【问题讨论】:

    标签: python pyqt4 qstring qlineedit


    【解决方案1】:

    颜色使用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_())
    

    【讨论】:

    • @learncode QLineEdit 是 QWidget 的孩子,因此会有父方法。
    • @learncode 我使用 Qt Widget 文档,并查看所有成员列表,包括继承的成员:doc.qt.io/qt-5/qlineedit-members.html
    【解决方案2】:

    你可以改变颜色:

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

    字体大小:

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

    【讨论】:

    • 谢谢!我可以改变颜色,但不能改变字体?
    猜你喜欢
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 2016-06-03
    • 2011-03-17
    • 2013-09-24
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    相关资源
    最近更新 更多