PyQt4 QPalette not working

自古美人都是妖i 提交于 2019-12-14 01:25:25

问题


    {btn = QtGui.QPushButton('Button', self)

    palettes = btn.palette()
    palettes.setColor(btn.backgroundRole(),QtCore.Qt.green)

    btn.setPalette(palettes)
    btn.setAutoFillBackground(True)}

Using btn.backgroundRole() only provides green border to the button.
Using btn.foregroundRole() changes the text color of the button
Using btn.windowRole() gives me error

TypeError: arguments did not match any overloaded call:
QPalette.setColor(QPalette.ColorGroup, QPalette.ColorRole, QColor): argument 1 has unexpected type 'str'
QPalette.setColor(QPalette.ColorRole, QColor): argument 1 has unexpected type 'str'

Tried QtGui.QPalette.Background in place of btn.windowRole() but does nothing
Also tried QtGui.QPalette.Base but does nothing
And also QtGui.QPalette.Window does nothing

How can I change button color using palettes?
I am unable to rectify the problem. I am using windows 7, PyQt4 and Python 3.4


回答1:


You can read in Qt documentation about QPalette :

Warning: Some styles do not use the palette for all drawing, for instance, if they make use of native theme engines. This is the case for both the Windows XP, Windows Vista, and the Mac OS X styles.

Windows 7 style also does not use palette colors for buttons. You should use stylesheets to give a color to your button. If you use some other theme than Windows 7 style, you can use QPalette.Button role to change the button color.



来源:https://stackoverflow.com/questions/28257482/pyqt4-qpalette-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!