Issue
What I have on hand are two widgets, one of which's bg color I'd like to be set as the text color of the other. After going through the docs, I couldn't find any method that'd yield me the color of a specific color role from a palette.
The closest method in terms of relevancy simply returns a QBrush QPalette.colorRole
, which I don't see as to how it can help here.
Say I had something of the form
widget1 = QWidget()
palette1 = widget1.palette()
palette1.setColor(QPalette.Text, QColor('#FFAABBCC'))
widget1.setPalette(palette1)
widget2 = QWigdet()
palette2 = widget2.palette()
palette2.setColor(widget1s text color) #assume this is what the command tries to do
I'd like for a way to set palette2
's QPalette.Base
color role for instance, to be set to the same color as palette1
's QPalette.Text
role. How would I go about doing this?
Solution
While QPalette accepts QColors, its roles are all based on QBrush; unless the brush uses a gradient or an image, the brush color()
will return a valid QColor.
Setting the color of another role is as simple as setColor(role, color)
.
Since you are already using palettes, it's not necessary to pass through the color, and you can just assign the original brush with setBrush(role, brush)
:
widget1 = QWidget()
palette1 = widget1.palette()
palette1.setColor(QPalette.Text, QColor('#FFAABBCC'))
widget1.setPalette(palette1)
widget2 = QWigdet()
palette2 = widget2.palette()
palette2.setBrush(QPalette.Base, palette1.brush(QPalette.Text))
# alternatively
palette2.setBrush(QPalette.Base, palette1.text())
widget2.setPalette(palette2)
The direct getter functions (like text()
or window()
) always return a QBrush and they are shortcuts to brush(role)
for the current color group. Whenever you need to specify different color groups, you have to use the appropriate brush()
/color()
and setBrush()
/setColor()
functions that accept the group, otherwise use setCurrentColorGroup()
before that if you need multiple calls on the same group.
Answered By - musicamante
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.