gpt4 book ai didi

c++ - 获取 QPushButton 按下的背景颜色

转载 作者:行者123 更新时间:2023-11-30 05:29:14 26 4
gpt4 key购买 nike

如何获取 QPushButton 按下时的背景颜色?

if (isDown())
_BGColor = <pressed background color>; // how to get this???
else
_BGColor = palette().color(QPalette::Background); // this is to get the idle backcolor

提前致谢!!!

最佳答案

很难(如果不是不可能的话)找到一种方法来在按下按钮时获取按钮的背景颜色,因为这取决于样式,并且不能保证样式符合调色板。

但是我建议两种不同的方法:

  1. 您可以使用样式表(更简单)设置​​自己的背景颜色,或者使用样式或重新实现 paintEvent() 自己实现按钮的绘制。参见 Customizing QPushButton

  2. 要在按钮上绘制反色,可以为 Painter 设置合成模式以获得反色。

例如:

painter.setPen(QColor(255, 255, 255));
painter.setCompositionMode(QPainter::RasterOp_SourceAndNotDestination);

(注意使用这个例子,中间灰度(128,128,128)的反色是完全一样的颜色)

参见 QPainter::CompositionMode

关于c++ - 获取 QPushButton 按下的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36492460/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com