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

标签 c++ qt qcolor qpalette

如何获取 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/

相关文章:

c++ - 当我尝试清理STL vector 时发生未处理的异常

c++ - 将时间 API 从 Linux 移植到 Visual Studio 2008

c++ - 是否可以将文件路径传递给 Qt 中的 setStyleSheet()

qt - 将自定义颜色的按钮设置为禁用样式

c++ - 比较两个 QColor 对象的颜色

c++ - OpenGL VBO : Drawing a sphere

c++ - boost::asio::ssl::context 可以在多个 SSL 流之间共享吗?

c++ - 具有默认目录的 QFileDialog

qt - 我应该如何在 QT4 中使用带有 qmake 而没有 cmake 的 PCL

qt - 如何使用 QPalette 自定义 Qpushbutton 上的文本?