我的QGraphicsItem项目没有被鼠标注意到

My QGraphicsItem items are not noticed by the mouse

本文关键字:鼠标 注意到 QGraphicsItem 项目 我的      更新时间:2023-10-16

我正在尝试移动我的项目...唯一的方法是,如果我遍历所有项目并检查鼠标位置:

class Item : public QGraphicsItem
{
    Item() { setFlag(ItemIsMovable); setFlag(ItemIsSelectable); scale = 10; }
    QRectF boundingRect() const;
    void paint(QPainter *painter);   // had to implement this because I don't know how to get the QStyleOptionGraphicsItem to call the paint below
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
               QWidget *widget) {paint(painter);}   // never called
    void update() {}
    int scale, x, y;
};
// scale is Item property set by caller, x and y also set by caller based on viewport position of item center
QRectF Item::boundingRect() const
{
    return QRectF(x - 30 - scale, y - 30 - scale,
                  30 + 3 * scale, 20 + 3 * scale);
}
void Item::paint(QPainter *painter)
{
    update();
    painter->drawRect(boundingRect());
}
class CollectionView : public QGraphicsView
{
    Q_OBJECT    
public:
    CollectionView(QWidget *parent = 0);    
    QList<Item*> *m_items;
protected:
    virtual void paintEvent(QPaintEvent * event);
    virtual void mousePressEvent(QMouseEvent * event);
    virtual void mouseReleaseEvent(QMouseEvent *event);
};
CollectionView::CollectionView(QWidget *parent)
    : QGraphicsView(parent)
{
    QGraphicsScene *s = new QGraphicsScene(this);
    setScene(s);
    setViewportUpdateMode(BoundingRectViewportUpdate);
    m_items = new QList<Item*>();
}
void CollectionView::paintEvent(QPaintEvent * /* event */)
{
    QPainter painter(this->viewport());
    for(int i = 0; i< m_items->size(); i++)
    {
        Item* item = m_items->at(i);
        //scene()->addItem(item);   // this crashes
        item->paint(&painter);
    }
}
void CollectionView::mousePressEvent(QMouseEvent* event)
{
    // if I add this and comment the rest, items don't move
    //QGraphicsView::mousePressEvent(event);
    foreach (QGraphicsItem *item, this->items(event->pos()))
    { /* never gets inside */ }
    foreach (QGraphicsItem *item, this->items(event->globalPos()))
    { /* never gets inside */ }
    // the following works though:
    for (int i = 0; i < m_items->size(); i++)
    {
        Item* currentItem = m_items->at(i);
        if(!currentItem->boundingRect().contains(event->pos()))
            continue;
        if (event->button() == Qt::LeftButton)
            { /* I can get the item index and its relative position to mouse
                 then pass this info to the mouseReleaseEvent, and it works */ }
        break;
    }
}

如果我可以使用QGraphicsView/QGraphicsScene/QGraphicsItem方法来移动项目或获取上下文菜单,那将是最好的...我还没想通怎么做。

但是,如果我必须实现鼠标操作,最好是我可以遍历在鼠标位置找到的项目,而不是列表中的所有项目(可能更大)。

为什么我的尝试不起作用? (或者,我怎样才能让QGraphicsScene/QGraphicsView完成所有工作并移动项目,而无需编写任何代码?这就是ItemIsMovable的目的......对吧?)

更新:添加了paintEvent的代码...不幸的是,我无法打电话给

空隙涂料(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

油漆的版本 - 它可能会被类似 scene()->addItem(item); 的东西使用 - 但该特定调用使程序崩溃......上面的油漆方法从未被调用过,但我知道这是他QGraphicsItem的官方油漆方法......这么简单的代码却这么乱。

正如我们在注释中建立的那样,问题是从 boundingRect( ) 函数返回的值。

QGraphicsItem 的边界矩形定义项的本地坐标。如果该项是常规的,匹配矩形,则只需要实现 boundingRect()。这用于碰撞检测,其中包括检测鼠标在项目上。

如果你有一个非规则(矩形)对象,并且想要更精细的碰撞检测,那么除了 boundingRect() 之外,还要实现 shape() 函数。两者都位于项目的本地坐标中。

首先,尝试使用一个简单的项目和一个基本的图形场景和视图。诸如此类:

    class GraphicsItem : public QGraphicsItem
{
public:
    GraphicsItem(QWidget* parent) {
        setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
    }
    QRectF boundingRect() const
       {
           qreal penWidth = 1;
           return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,
                         20 + penWidth, 20 + penWidth);
       }
       void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                  QWidget *widget)
       {
           painter->drawRoundedRect(-10, -10, 20, 20, 5, 5);
       }
};

这是一个基本的项目,当你使用基本的QGraphicsItem和QGraphicsScene时,将会移动。

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QGraphicsScene scene;
        scene.addItem(new GraphicsItem(NULL));
        QGraphicsView view(&scene);
        view.show();
        return a.exec();
   }

如果代码中的问题似乎您的项目没有移动它,则可能是您的 MousePressEvent。