Qt如何使用带有QColor的RGB颜色创建QBrush,并在以后进行更改
Qt How to create a QBrush using a RGB Color with QColor and change it later?
现在我用它来创建一个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);
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 使用std::multimap迭代器创建std::list
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 使用CMake创建QML插件
- 如何在c++中为模板函数实例创建快捷方式
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 试图在visual studio上用C++创建一个桌面应用程序
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 如何在C++20中创建模板别名的推导指南
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 如何创建一个空的全局类并在启动时实例化它
- 无法创建抽象类的实例
- 链接到自行创建的dll失败
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++类内存结构中创建"spacer"?
- 终端不会为C++文件创建.exe文件吗
- Qt如何使用带有QColor的RGB颜色创建QBrush,并在以后进行更改