gpt4 book ai didi

c++ - itemAt 不返回自定义 qGraphicsItem

转载 作者:太空狗 更新时间:2023-10-29 21:24:05 31 4
gpt4 key购买 nike

我有一个自定义的 qGraphicsScene 实现和一个我点击的自定义 qGraphicsItem,但是 itemAt 函数从不返回值,即使我相当确定我正在点击该项目。

void VScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if ((vMouseClick) && (event->pos() == vLastPoint)) {
QGraphicsItem *mod = itemAt(event->pos(), QTransform());
if (mod) { // Never returns true
// ...
}
}
}

为清楚起见,模块添加在以下代码中:

void VScene::addModule(QString modName, QPointF dropPos)
{
VModule *module = new VModule();
addItem(module);
// the QPointF value comes from an event in mainWindow, the coordinate is mapped to my scene.
module->setPos(dropPos);
}

...这是我编写的自定义 qGraphicsItem。

VModule.h:

class VModule : public QObject, public QGraphicsItem
{
public:
explicit VModule();
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

private:
qreal w;
qreal h;
int xAddr;
int yAddr;
QPolygonF baseShape;
}

VModule.cpp:

VModule::VModule()
{
w = 80;
h = 80;
xAddr = w / 2;
yAddr = h / 2;

// Use the numbers to create a number of polygons
QVector<QPointF> basePoints = { QPointF(0.0, 0.0),
QPointF(xAddr, yAddr),
QPointF(0.0, yAddr * 2),
QPointF(-xAddr, yAddr) };
baseShape = QPolygonF(basePoints);
}

QRectF VModule::boundingRect() const
{
return QRectF(-xAddr, 0, w, h);
}

void VModule::paint(QPainter *painter, const QStypeOptionGraphicsItem *option, QWidget *widget)
{
// brushes and so on are set
// ...

painter->drawPolygon(baseShape, qt::OddEvenFill);

// there are other polygons are drawn in the same way as above
}

我的实现有什么问题吗?有什么我想念的吗?在此先感谢您的帮助。

最佳答案

您正在以项目坐标而不是场景坐标查询场景。使用:

...
QGraphicsItem *mod = itemAt(event->scenePos());
...

关于c++ - itemAt 不返回自定义 qGraphicsItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16919819/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com