Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.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++ 设置位置时Qt remove object with contains()_C++_Qt - Fatal编程技术网

C++ 设置位置时Qt remove object with contains()

C++ 设置位置时Qt remove object with contains(),c++,qt,C++,Qt,我有一个场景,在场景中我有椭圆(圆),我用setPos()更改位置,因此当我稍后询问它的位置时,我不会得到0,0坐标,但现在当我要删除对象时,成员函数contains()的计算结果永远不会为true,这是可以理解的。问题是,如何获得场景坐标或对象坐标,以便在单击对象时获得contains()member函数的真实计算值。我已经尝试了mapToScene(),mapFromScene(),但都没有用。(仍然有点迷失在Qt坐标系中) 代码示例: void MyQGraphicsView::mouse

我有一个场景,在场景中我有椭圆(圆),我用
setPos()
更改位置,因此当我稍后询问它的位置时,我不会得到0,0坐标,但现在当我要删除对象时,成员函数
contains()
的计算结果永远不会为true,这是可以理解的。问题是,如何获得场景坐标或对象坐标,以便在单击对象时获得
contains()
member函数的真实计算值。我已经尝试了
mapToScene()
mapFromScene()
,但都没有用。(仍然有点迷失在Qt坐标系中)

代码示例:

void MyQGraphicsView::mousePressEvent(QMouseEvent * e)
{
    QPointF pt = mapToScene(e->pos());
    if(e->button()==Qt::RightButton)
    {
        // remove point
        QList<QGraphicsItem *> listIt = scene->items();
        QList<QGraphicsItem *>::const_iterator stlIter;
        QGraphicsItem * itemToRemove = NULL;
        QGraphicsEllipseItem it; // for type checking
        for(stlIter = listIt.begin(); stlIter != listIt.end(); ++stlIter)
        {
            // if it has the expected type and the point is inside (type checking is redundant)
            if(((*stlIter)->type() == it.type()) && ((*stlIter)->contains(pt))){
                // contains(pt) is never true - understandably
                itemToRemove = *stlIter;
                break;
            }
        }
        if(itemToRemove != NULL) scene->removeItem(itemToRemove);
    }else{ // leftClick to add ellipse
        double rad = 10;
        QGraphicsEllipseItem* pEllipse = scene->addEllipse(-rad, -rad, rad*2.0, rad*2.0, QPen(Qt::red,0), QBrush(Qt::red,Qt::SolidPattern));
        pEllipse->setPos(pt.x(), pt.y()); // set the postion so it does not return 0,0
    }
}
void MyQGraphicsView::mousePressEvent(QMouseEvent*e)
{
QPointF pt=maptosene(e->pos());
如果(e->button()==Qt::RightButton)
{
//删除点
QList listIt=场景->项目();
常量迭代器stlIter;
QGraphicsItem*itemToRemove=NULL;
QGraphicsEllipseItem it;//用于类型检查
对于(stlIter=listIt.begin();stlIter!=listIt.end();++stlIter)
{
//如果它具有预期类型且点在内部(类型检查是冗余的)
如果((*stlIter)->type()==it.type())&&(*stlIter)->contains(pt))){
//contains(pt)永远都不是真的,这是可以理解的
itemToRemove=*stlIter;
打破
}
}
如果(itemToRemove!=NULL)场景->移除项目(itemToRemove);
}else{//leftClick添加椭圆
双rad=10;
QGraphicsEllipseItem*pEllipse=scene->addEllipse(-rad,-rad,rad*2.0,rad*2.0,QPen(Qt::red,0),QBrush(Qt::red,Qt::SolidPattern));
pEllipse->setPos(pt.x(),pt.y());//设置位置,使其不返回0,0
}
}

QGraphicsItem::contains方法采用局部坐标中的点,即以
(0,0)
作为
QGraphicsItem
的中心的坐标

您正在使用全局场景坐标中的点。 要获取给定
QGprahicsItem
的局部坐标中的点,可以使用
QGraphicsItem::mapFromScene(const QPointF&point)
方法

您可能希望执行以下操作:

for(Object& obj : objects)
    if(obj.contains(obj.mapFromScene(point)))
        // do stuf because point is inside obj
资料来源:


这很有效!准确地说,示例代码
(*stlIter)->包含((*stlIter)->mapFromScene(pt))