Qt如何使用带有QColor的RGB颜色创建QBrush,并在以后进行更改

Qt How to create a QBrush using a RGB Color with QColor and change it later?

本文关键字:QBrush 创建 何使用 QColor 颜色 RGB Qt      更新时间:2023-10-16

现在我用它来创建一个QBrush:

QBrush *goldBrush = new QBrush(QColor(212,175,55));
scene->addRect(0,415,20,50,noPen,*goldBrush);

但显然这会泄露记忆。

否则你怎么能做到呢?我试过这个:

QBrush greyBrush(QColor(212,175,55));
greyBrush.setColour(QColor(120,60,55))

但这也没有奏效。我想能够宣布画笔为一种颜色,然后能够改变它。

编辑:完全问题我的坏。

更改画笔颜色的唯一方法是通过QBrush::setColor。笔刷将复制您指定的颜色,而不是引用。

QBrush my_brush;
QColor red(Qt::red);
my_brush.setColor(red); // my_brush has its own color member internally
                        // and _not_ a reference to red

也许你已经习惯了其他编程语言,比如Java,基本上所有的东西都是参考。在C++中存在值语义。

不要忘记设置brush style

QPainter painter(this);
QBrush brush;
QColor brushColor;
brushColor.setRgb(192 ,237, 166);
brush.setColor(brushColor);
brush.setStyle(Qt::SolidPattern);