【问题标题】:Dragging QPixmaps inside QGraphicsScene: how to avoid 'auto' not allowed in lambda parameter在 QGraphicsScene 中拖动 QPixmap:如何避免 lambda 参数中不允许使用“自动”
【发布时间】:2019-03-18 18:01:44
【问题描述】:

我正在尝试实现自定义QGraphicsScene,当我们按下左键时,它允许拖动一个项目,为此我使用QDrag 并传递项目数据,然后我覆盖dropEvent 事件,其中我得到元素和dropEvent 新父母。我认为 QGraphicsPixmapItem 在另一个项目之上可能会很棘手,所以最好的选择是将其设置为 parentItem

但是,我收到以下错误 'auto' not allowed in lambda parameter 并且不知道确切原因

graphicsscene.h

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event) override;

graphicsscene.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 吗?
  • 您好 Guillaume Racicot,您是什么意思?在我的 .pro 文件中,我有这个 QMAKE_CXXFLAGS += -std=gnu++11
  • 我的意思是,你能更新到 C++14 吗?
  • 感谢工作:)!你能解释一下为什么吗?

标签: c++ c++11 lambda qgraphicsview qgraphicsscene


【解决方案1】:

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())
    }
);

【讨论】:

  • 非常感谢 Guillaume Racicot 的非常清楚的解释! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-26
  • 1970-01-01
  • 1970-01-01
  • 2014-12-11
  • 2020-04-01
  • 1970-01-01
相关资源
最近更新 更多