GraphicsView 上的 Qt QColor 和 QBrush?

Qt QColor and QBrush on a GraphicsView?

如何使用 RBG 值将自定义颜色设置为 QBrush?然后在GraphicsView上用画笔画一个矩形。

到目前为止我已经尝试过:

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

错误信息是:

error: C2664: 'QGraphicsRectItem *QGraphicsScene::addRect(qreal,qreal,qreal,qreal,const QPen &,const QBrush &)' : cannot convert argument 6 from 'QBrush *' to 'const QBrush &' Reason: cannot convert from 'QBrush *' to 'const QBrush' No constructor could take the source type, or constructor overload resolution was ambiguous

然后当我将第一行替换为:

const QBrush *goldBrush = new QBrush(QColor(212,175,55));

消息仅略微更改为:

error: C2664: 'QGraphicsRectItem *QGraphicsScene::addRect(qreal,qreal,qreal,qreal,const QPen &,const QBrush &)' : cannot convert argument 6 from 'const QBrush *' to 'const QBrush &' Reason: cannot convert from 'const QBrush *' to 'const QBrush' No constructor could take the source type, or constructor overload resolution was ambiguous

您应该将 "scene->addRect(0,415,20,50,noPen,goldBrush);" 替换为 "scene->addRect(0,415,20,50,noPen,*goldBrush);"。您这样做的方式是将地址传递给内存中的某个位置,而不是对变量的引用。