- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我找不到我做错了的地方。我想在发生特定事件时更改项目(QGraphicsRectItem)的颜色。事实是,似乎一旦调用了覆盖绘画方法,无论如何颜色都不会改变。这是我所做的简单代码:
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::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);
}
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)
之后,您的圈子没有被涂成红色。如果是这种情况,则问题是您在Qt::NoPen
函数中强行使用了特定的笔(Qt::red
)和画笔(paint
),而没有使用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::setPen
和
Item::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();
}
关于c++ - 如何更改QGraphicsRectItem的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62081156/
我正在从 QGraphicsRectItem 创建对象并添加到 Qgraphicscene(scene)。我想获取对象 (qgraphicsrectitem) 的每个 movement(pos),以便
我实际上正在实现一个类,该类允许用户在按下鼠标时绘制任意数量的矩形。 这是处理这种情况的 QGraphicsScene 的代码: void ImageGraphicsSceneW::mousePres
我一辈子都弄不明白这个问题,但我已将其归结为一个自给自足的问题。 我想做的是围绕在 QGraphicsScene 中选择的项目绘制一个 QGraphicsRectItem。绘制矩形后,可以以将所有项目
我在 qgraphicsScene 上绘制了一个 qgraphicsRectItem。对于鼠标事件,它会在场景上移动、调整大小和重新定位,即选择项目、mousePress 和 mouseMove。我怎
我一直在尝试找到一种方法来用任何颜色填充 QGraphicsRectItem 的实例,比如说红色,但是我没有找到一个明确的方法来完成这个天真的任务,如果你能的话那就太好了提供一些提示、链接等。 非常感
我试图在将 QGraphicsRectItem 添加到场景后移动它。它会移动,但与鼠标指针有一定的偏移量。我认为它只是将鼠标指针位置添加到其原始位置。我不知道如何解决这个问题。 这是我的代码: cla
我试图在用户移动 QGraphicsRectItem 时获取它的子类的 boundingRect() 。但是,随着矩形在场景中移动,boundingRect() 返回的矩形始终返回边界矩形的原始值。下
我想要一个在线监控系统,可以判断形状当前的位置,但是我得到了非常奇怪的项目坐标,而且每次我创建新的并拖动它时它的尺寸都会增加 1。 初始位置( map 大小为 751 x 751,通过输出到 qDeb
我正在逐步完成一个使用 Qt 和 C++ 的初学者(非基于类的)项目。该项目的主要目标是学习设计模式。但是在我开始学习如何使用和实现不同的设计模式以改进代码之前,我必须先有一个没有任何设计模式的工作实
我正在尝试创建一个可调整大小的 QGraphicsRectItem,并可以选择不同的绘制样式。 如果我创建一个仅具有调整大小功能的简单矩形,那么它会按预期工作: class Rectangle(QtW
我有 QGraphicsView、QGraphicsScene 和 QGraphicsRectItem。QGraphicsScene 中的 QGraphicsRectItem 和 QGraphicsV
我编写了这段代码来将 bullet 旋转到 enemy: void Bullet::turnToFutureLocation(Enemy *enemy) { QLineF ln(mapToSc
我正在创建排序算法的可视化。我有一个 QGraphicsRectItems vector ,它们是在应用程序启动时生成的具有不同高度和相同宽度的列。它们被打乱,然后添加到 QGraphicsScene
如何在 Qt 中的 qgraphicsrectitem 中制作自动收报机我试着跟随 rect = new QGraphicsRectItem; text = new QGraphicsText
如果我越过像 QGraphicsRectItem 这样的矩形,我想更改光标。 我有一个继承自 QGraphicsView 的类,矩形显示在 QGraphicScene 中。 我使用 eventFilt
我想做的是: 我有一个带 QGraphicsView 的小 GUI。在此图形 View 中,我加载了一张图片: // m_picture is QPixmap // image is QImage /
我第一次尝试 PyQT,目的是最终创建一些数据的非常基本的图形表示。 我目前正在尝试将两个 QGraphicsTextItem 装在 QGraphicsRectItem 中,我对看到的结果有一些疑问。
我是一名优秀的程序员,十分优秀!