如何获取 QPushButton 按下时的背景颜色?
if (isDown())
_BGColor = <pressed background color>; // how to get this???
else
_BGColor = palette().color(QPalette::Background); // this is to get the idle backcolor
提前致谢!!!
最佳答案
很难(如果不是不可能的话)找到一种方法来在按下按钮时获取按钮的背景颜色,因为这取决于样式,并且不能保证样式符合调色板。
但是我建议两种不同的方法:
您可以使用样式表(更简单)设置自己的背景颜色,或者使用样式或重新实现 paintEvent() 自己实现按钮的绘制。参见 Customizing QPushButton
要在按钮上绘制反色,可以为 Painter 设置合成模式以获得反色。
例如:
painter.setPen(QColor(255, 255, 255));
painter.setCompositionMode(QPainter::RasterOp_SourceAndNotDestination);
(注意使用这个例子,中间灰度(128,128,128)的反色是完全一样的颜色)
关于c++ - 获取 QPushButton 按下的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36492460/