gpt4 book ai didi

c++ - 鼠标没有注意到我的 QGraphicsItem 项目

转载 作者:行者123 更新时间:2023-11-28 02:31:09 26 4
gpt4 key购买 nike

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

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的代码...不幸的是我无法调用

void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *小部件);

paint 的版本 - 它可能会被 scene()->addItem(item); 之类的东西使用 - 但那个特定的调用会使程序崩溃......上面的 paint 方法永远不会调用,但我知道这是他 QGraphicsItem 的官方绘画方法......如此简单的代码却如此困惑。

最佳答案

正如我们在评论中所确定的那样,问题是从 boundingRect( ) 函数返回的值。

QGraphicsItem 的边界矩形定义了项目的本地坐标。如果项目是规则的,匹配一个矩形,你只需要实现 boundingRect()。这用于碰撞检测,除其他外,检测鼠标是否在项目上。

如果您有一个非常规(矩形)对象并希望进行更细粒度的碰撞检测,请实现 shape()功能,除了 boundingRect()。两者都在项目的本地坐标中。

关于c++ - 鼠标没有注意到我的 QGraphicsItem 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28966602/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com