Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ itemAt不返回自定义qGraphicsItem_C++_Qt_Qgraphicsscene - Fatal编程技术网

C++ itemAt不返回自定义qGraphicsItem

C++ itemAt不返回自定义qGraphicsItem,c++,qt,qgraphicsscene,C++,Qt,Qgraphicsscene,我有一个自定义的QGraphicscene实现和一个自定义的qGraphicsItem,我单击它,但是itemAt函数从不返回值,即使我相当确定我正在单击该项 void VScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { if ((vMouseClick) && (event->pos() == vLastPoint)) { QGraphicsItem *mod = itemAt(

我有一个自定义的QGraphicscene实现和一个自定义的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
}
VModule::VModule()
{
w=80;
h=80;
xAddr=w/2;
yAddr=h/2;
//使用这些数字可以创建多个多边形
QVector基点={QPointF(0.0,0.0),
QPointF(xAddr、yAddr),
QPointF(0.0,yAddr*2),
QPointF(-xAddr,yAddr)};
baseShape=QPolygonF(基点);
}
QRectF VModule::boundingRect()常量
{
返回QRectF(-xAddr,0,w,h);
}
void VModule::paint(QPainter*painter,const QStypeOptionGraphicsItem*选项,QWidget*小部件)
{
//设置了笔刷等
// ...
画师->绘制多边形(基本形状,qt::OddEvenFill);
//其他多边形的绘制方式与上述相同
}

我的实现有什么问题吗?我有什么遗漏吗?提前感谢您的帮助。

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

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

可能是一些坐标转换问题(例如,您得到绝对屏幕坐标,但场景需要自己的坐标),而我不是sure@Lol4t0事件是在场景中创建和处理的,所以我认为绝对坐标不是问题。AFAIK QGraphicsItem也使用场景坐标?哈,应该注意到了,谢谢。编译器不会接受省略转换参数,知道为什么吗?转换应该来自视图。因此,如果
qgraphicsceneumouseevent::widget()
成员是
QGraphicsView
,您应该测试它,如果是,则从中提取
transform()