c++ - 具有多种颜色背景的 QPalette

标签 c++ qt

我知道我们可以使用 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/

相关文章:

c++ - OpenCL 程序构建错误 : source file is not valid UTF-8

c++ - 日期格式的时间标记?

c++ - 无损转换 std::string 和 QByteArray 的正确方法

c++ - QT 中的 SQLite CREATE 查询

qt - 如何在QTableWidget的单元格中编辑多行文本?

c++ - 发出 textChanged() 信号时获取 QTextEdit 更改

c++ - SAPI:应用程序不说话

c++ - directx 上的一切都消失了

c++ - Qt C++ : Connecting value changing in two spinboxes

c++ - 在 C++ 中返回 int 进行错误检查是不是很糟糕