C++ 在QGraphicscene中拖动QPixmaps:如何避免';自动';lambda参数中不允许使用
我正在尝试实现一个自定义的C++ 在QGraphicscene中拖动QPixmaps:如何避免';自动';lambda参数中不允许使用,c++,c++11,lambda,qgraphicsview,qgraphicsscene,C++,C++11,Lambda,Qgraphicsview,Qgraphicsscene,我正在尝试实现一个自定义的qgraphicscene,当我们按下左键时,它允许拖动一个项目,我使用QDrag并传递项目数据,然后覆盖dropEvent事件,从中获取元素和dropEvent新父项。我认为在另一个项目上添加QGraphicsPixmapItem可能很棘手,因此最好的选择可能是将其设置为parentItem 但是,我在lambda参数中不允许出现以下错误'auto',我不知道确切原因 graphicscene.h protected: void mousePressEvent
qgraphicscene
,当我们按下左键时,它允许拖动一个项目,我使用QDrag
并传递项目数据,然后覆盖dropEvent
事件,从中获取元素和dropEvent
新父项。我认为在另一个项目上添加QGraphicsPixmapItem
可能很棘手,因此最好的选择可能是将其设置为parentItem
但是,我在lambda参数中不允许出现以下错误'auto',我不知道确切原因
graphicscene.h
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
graphicscene.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);
}
}
void graphicscene::mousePressEvent(qgraphicscenemouseevent*事件)
{
自动its=items(QRectF(event->scenePos()-QPointF(1,1),QSize(3,3));
auto val=std::find_if(its.constBegin()、its.constEnd()、[](auto-const&it){//type()>QGraphicsItem::UserType;
});
if(val==its.constEnd())
返回;
如果(事件->按钮()==Qt::RightButton){
showContextMenu(事件->场景());
}
否则{
创建拖动(事件->场景(),事件->小部件(),*val);
}
}
感谢您对此的了解。C++11不支持通用lambda。这意味着您不能使用类型为auto
的参数
只需更新到C++14:
QMAKE_CXXFLAGS += -std=c++14
这至少需要GCC 5
通用lambda比简单lambda更难支持,因为它们需要一个模板作为lambda闭包来实现
如果要继续使用C++11,则必须直接指定函数参数的类型:
auto val = std::find_if(
its.constBegin(),
its.constEnd(),
[](Item const& it) { // let Item be the
// type of (*its.constBegin())
}
);
你只限于C++11吗?嗨,纪尧姆·拉西科,你是什么意思?在我的.pro文件中,我有一个QMAKE_CXXFLAGS+=-std=gnu++11
我的意思是,你能更新到C++14吗?谢谢你的帮助:)!你能解释一下原因吗?非常感谢纪尧姆·拉西科的非常清楚的解释!:)