我手头有两个小部件,其中一个是bg颜色,我想将其设置为另一个的文本颜色。在浏览了这些文档之后,我找不到从调色板中获得特定颜色角色的颜色的任何方法。
在关联方面,最接近的方法只是返回一个QBrush QPalette.colorRole,我看不出它在这里有什么帮助。
说我有某种形式的东西
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例如,我希望找到一种方法来设置palette2的QPalette.Base颜色角色,将其设置为与palette1的QPalette.Text角色相同的颜色。
发布于 2022-02-01 20:02:44
虽然QPalette接受QColors,但它的角色都是基于QBrush的;除非画笔使用梯度或图像,否则画笔color()将返回有效的QColor。
设置另一个角色的颜色与setColor(role, color)一样简单。
由于您已经在使用调色板,所以没有必要通过颜色传递,您只需使用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)直接getter函数(如text()或window())总是返回一个QBrush,它们是当前颜色组的brush(role)快捷键。每当需要指定不同的颜色组时,都必须使用接受组的适当brush()/color()和setBrush()/setColor()函数,否则,如果需要对同一组进行多次调用,则在此之前使用setCurrentColorGroup()。
https://stackoverflow.com/questions/70946067
复制相似问题