我正在尝试实现自定义QGraphicsScene
,当我们按下左键时,它允许拖动一个项目,我使用QDrag
并传递项目数据,然后我覆盖dropEvent
事件,我得到元素和dropEvent
新父项。我认为QGraphicsPixmapItem
在另一个项目之上可能会很棘手,所以也许最好的选择是将它设置为parentItem
。
但是,我得到以下错误'auto' not allowed in lambda parameter
,并不知道究竟为什么
qgraphicsscene.h
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
qgraphicsscene.cpp
void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
auto its = items(QRectF(event->scenePos() - QPointF(1,1), QSize(3,3)));
auto val = std::find_if(its.constBegin(), its.constEnd(), [](auto const& it){ // <-- ERROR HERE
return it->type() > QGraphicsItem::UserType;
});
if(val == its.constEnd())
return;
if(event->button() == Qt::RightButton){
showContextMenu(event->scenePos());
}
else{
createDrag(event->scenePos(), event->widget(), *val);
}
}
感谢您对此有任何见解。
C ++ 11不支持通用lambdas。这意味着您不能使用auto
类型的参数。
只需更新到C ++ 14:
QMAKE_CXXFLAGS += -std=c++14
这至少需要GCC 5。
与简单的lambda相比,通用lambdas支持起来有点棘手,因为它们需要将模板实现为lambda闭包。
如果你想继续使用C ++ 11,你必须直接指定函数参数的类型:
auto val = std::find_if(
its.constBegin(),
its.constEnd(),
[](Item const& it) { // let Item be the
// type of (*its.constBegin())
}
);