Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.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++ 从QGraphicsPixmapItem派生的类不';不接受标志,因此无法将其设置为可移动_C++_Qt - Fatal编程技术网

C++ 从QGraphicsPixmapItem派生的类不';不接受标志,因此无法将其设置为可移动

C++ 从QGraphicsPixmapItem派生的类不';不接受标志,因此无法将其设置为可移动,c++,qt,C++,Qt,我使用的是qgraphicscene,我想让一些物品在移动时发出信号 不幸的是,QGraphicsPixmapItem没有任何信号,因此我将其子类化: class InteractiveGraphicsPixmapItem : public QObject, public QGraphicsPixmapItem { Q_OBJECT public: InteractiveGraphicsPixmapItem(); InteractiveGraphicsPixmapIt

我使用的是
qgraphicscene
,我想让一些物品在移动时发出信号

不幸的是,
QGraphicsPixmapItem
没有任何信号,因此我将其子类化:

class InteractiveGraphicsPixmapItem : public QObject, public QGraphicsPixmapItem
{
    Q_OBJECT

public:
    InteractiveGraphicsPixmapItem();

    InteractiveGraphicsPixmapItem(QPixmap pm) : QGraphicsPixmapItem(pm)
    {
        setFlag(QGraphicsItem::ItemIsMovable, true);
        setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
    }

private:
    void mouseMoveEvent(QGraphicsSceneMouseEvent *);

signals:
    void moved_by_mouse(QPointF newpos);

};


InteractiveGraphicsPixmapItem::InteractiveGraphicsPixmapItem()
{
}

void InteractiveGraphicsPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *)
{
    emit moved_by_mouse(this->pos());
}
然而,它是不可移动的。 如果我在主程序中将其更改回
QGraphicsPixmapItem
,并调用
item->setFlags(QGraphicsItem::ItemIsMovable)它可以移动。对于我的自定义类,它没有

item = new InteractiveGraphicsPixmapItem(QPixmap(":/img/icon.png"));
scene->addItem(item);
item->setFlags(QGraphicsItem::ItemIsMovable);

A被问及可选择性,建议使用
setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton)添加到构造函数中。在我的情况下,它没有帮助。

如果重写mouseMoveEvent,并且不调用基类的函数,它将不会移动,您必须自己处理它

但是,这里只需要调用基类函数

void InteractiveGraphicsPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent* evt)
{
    QGraphicsPixmapItem::mouseMoveEvent(evt);
    emit moved_by_mouse(this->pos());
}
另外请注意,如果您覆盖其中一个鼠标事件(按下/释放/移动),您也应该处理其他鼠标事件