移动QGraphicsPixmapItem并获取itemChange事件

时间:2013-05-19 16:17:29

标签: qt qgraphicsview

我有一个QGraphicsPixmapItem我移动setFlag(QGraphicsItem::ItemIsMovable),我在项目上激活了ItemSendsScenePositionChanges标记但是当我移动项目时没有调用函数itemChange(GraphicsItemChange change, const QVariant &value)周围。

任何sugestions?

这是我的itemchange()代码:

#include "graphicspixmapitem.h"

GraphicsPixmapItem::GraphicsPixmapItem(QGraphicsItem *parent, QGraphicsScene *scene)
  : QGraphicsPixmapItem(parent)
{

}

void GraphicsPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
  //m_clicked_signal(this);
  //  emit clicked();
}
void GraphicsPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
  //m_clicked_signal(this);
    emit clicked();
}

#include <QDebug>
QVariant GraphicsPixmapItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
    if (change == ItemPositionChange) {
            qDebug() << "Position changed";
        }
    return QGraphicsItem::itemChange(change, value);
}

这是我的setFlags代码:

QPixmap Trackinfo(":/images/ScreenContacts.png");
    buf = addPixmap(Trackinfo);
    buf->setPos(0, 40);
    buf->setFlag(QGraphicsItem::ItemIsMovable);
    buf->setFlag(QGraphicsItem::ItemSendsScenePositionChanges);
    buf->setCacheMode(QGraphicsItem::ItemCoordinateCache);

1 个答案:

答案 0 :(得分:1)

您是否尝试在项目的mousePressEvent和mouseReleaseEvent上调用QGraphicsItem :: mousePressEvent和QGraphicsItem :: mouseReleaseEvent的默认实现?

如果你没有打电话给我,我怀疑你甚至无法用鼠标移动项目,因为你没有让Qt知道什么时候点击/释放了这个项目。

相关问题