Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 按下QPushButton背景色_C++_Qt_Qcolor_Qpalette - Fatal编程技术网

C++ 按下QPushButton背景色

C++ 按下QPushButton背景色,c++,qt,qcolor,qpalette,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 if(isDown()) _BGColor=;//如何得到这个??? 其他的 _BGColo

如何获得QPushButton的按下背景色

if (isDown())
    _BGColor = <pressed background color>; // how to get this???
else
    _BGColor = palette().color(QPalette::Background); // this is to get the idle backcolor
if(isDown())
_BGColor=;//如何得到这个???
其他的
_BGColor=palete().color(qpalete::Background);//这是为了得到空闲的背景色
提前感谢

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

然而,我建议两种不同的方法:

  • 您可以使用样式表(更简单)设置自己的背景色,也可以使用样式或重新实现paintEvent()自己实现按钮的绘制。看

  • 要使用反向颜色在按钮上绘制,可以将合成模式设置为“绘制者”以获得反向颜色

  • 例如:

    painter.setPen(QColor(255, 255, 255));
    painter.setCompositionMode(QPainter::RasterOp_SourceAndNotDestination);
    
    (请注意,使用此示例时,中灰色(128、128、128)的反向颜色完全相同)


    请参见

    欢迎来到Stackoverflow。你能解释一下你想做什么吗?看来换成这种颜色不是个好主意。也许我们可以找到更好的解决你的初始问题的方法?我有一个QPushButton子类,当按钮长按时,我想画一个背景颜色相反的圆圈,然后在里面是背景颜色的复选符号。我的另一个选择是擦除圆圈的一部分(使部分透明)来创建一个复选标记。我要做的是在我的按钮上加上一个覆盖圆,中间有一个支票形状的整体。它用于按钮的checked属性,比如有一个复选框,但我想将其呈现为绘图。您是否在子类的PaintEvent中编写了上述代码,尝试在PaintEvent中编写它并在mousePressEvent中调用Update函数。这将为您提供背景色的当前值。我已尝试使用
    update()
    和/或
    repaint()
    高于
    setDown(true)
    ,但它没有获得背景色的当前值。