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());
}
另外请注意,如果您覆盖其中一个鼠标事件(按下/释放/移动),您也应该处理其他鼠标事件