如何更改 QGraphicsRectItem 的颜色

How to change the color of a QGraphicsRectItem

我找不到哪里做错了。我想在特定事件发生时更改项目(QGraphicsRectItem)的颜色。事实是,似乎一旦调用override paint方法,颜色无论如何都不会改变。这是我所做的简单代码:

item.h

class Item : public QGraphicsRectItem
{
public:
    Item(QGraphicsView *graphView);
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) override;

private:
    QPointF newPos;
    QGraphicsView *graph;
};

item.cpp

Item::Item(QGraphicsView *graphWidget) : graph(graphWidget) { }

void Item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{
     painter->setPen(Qt::NoPen);
     painter->setBrush(Qt::black);
     painter->drawEllipse(-7, -7, 20, 20);
}

main.cpp

int main(int argc, char *argv[])
{
     srand(QDateTime::currentDateTime().toMSecsSinceEpoch());
     QApplication a(argc, argv);
     QGraphicsScene scene;
     QGraphicsView view(&scene);

     Item *item = new Item(&view);
     scene.addItem(item);
     item->setPos(0, 0);

     item->setBrush(Qt::red);
     item->update();

     view.show();
     return a.exec();
}

如果我没看错你的问题,问题是在item->setBrush(Qt::red)之后,你的圈子不是painter red。如果是这种情况,问题是您在 paint 函数中强制使用特定的笔 (Qt::NoPen) 和画笔 (Qt::red),而您没有使用 Item::pen()Item::brush() 来检索信息。相反,您可以执行以下操作:

Item::Item(QGraphicsView *graphWidget) : graph(graphWidget)
{
  setPen(Qt::NoPen);
  setBrush(Qt::black);
}

void Item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{
  Q_UNUSED(option);
  painter->setPen(pen());
  painter->setBrush(brush());
  painter->drawEllipse(-7, -7, 20, 20);
}

这样,您在构造函数中定义了默认的画笔和画笔,但您仍然可以使用 Item::setPenItem::setBrush 更改它们。此外,对于这个例子,你最好继承 QAbstractGraphicsShapeItem 但你必须实现 Item::boundingRect 功能。下面的例子输出了一个红色的圆圈(我怀疑你想要这样做)并且还用黑色绘制了轮廓(虽然这不是你想要的,但是为了表明笔也改变了):

#include <QApplication>
#include <QDateTime>
#include <QGraphicsRectItem>
#include <QGraphicsScene>
#include <QGraphicsView>

class Item : public QAbstractGraphicsShapeItem
{
public:
  Item(QGraphicsView *graphView);

  void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) override;

  virtual QRectF boundingRect() const override;

private:
  QPointF newPos;
  QGraphicsView *graph;
};

Item::Item(QGraphicsView *graphWidget) : graph(graphWidget)
{
  setPen(Qt::NoPen);
  setBrush(Qt::black);
}

void Item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{
  Q_UNUSED(option);
  painter->setPen(pen());
  painter->setBrush(brush());
  painter->drawEllipse(-7, -7, 20, 20);
}

QRectF Item::boundingRect() const
{
  double pw = pen().widthF() / 2;
  return QRectF(QPointF(-7 - pw, -7 - pw), QSizeF(20 + 2 * pw, 20 + 2 * pw));
}

int main(int argc, char *argv[])
{
  srand(QDateTime::currentDateTime().toMSecsSinceEpoch());
  QApplication a(argc, argv);
  QGraphicsScene scene;
  QGraphicsView view(&scene);

  Item *item = new Item(&view);
  scene.addItem(item);
  item->setPos(0, 0);

  item->setBrush(Qt::red);
  item->setPen(QPen(Qt::black));
  item->update();

  view.show();
  return a.exec();
}