我知道我们可以使用 QPalette 来设置 QLabel 的背景。但是我可以用 QPalette 绘制多种颜色的背景吗?例如,上面的一半是黑色,下面的一半是蓝色。 我找不到 setRect() 函数 QPalette。或者我应该使用其他类(class)?还是必须找画家画背景?
最佳答案
documentation for QGradient类状态:-
The QGradient class is used in combination with QBrush to specify gradient fills
因此,您可以首先创建渐变并将其设置为 QBrush
QLinearGradient linearGrad(QPointF(100, 100), QPointF(100, 200));
linearGrad.setColorAt(0, Qt::black);
linearGrad.setColorAt(0.5, Qt::blue);
您可以尝试在不同的停止处设置不同的颜色,范围从 0.0 到 1.0
使用渐变创建画笔...
QBrush brush(linearGrad);
documentation for QPalette状态:-
Colors and brushes can be set for particular roles in any of a palette's color groups with setColor() and setBrush().
因此,使用 QPalette 的 setBrush 函数,设置使用渐变创建的画笔:-
QPalette palette;
palette->setBrush(QPalette::Window, brush);
关于c++ - 具有多种颜色背景的 QPalette,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22712343/