鼠标没有注意到我的 QGraphicsItem 项目
My QGraphicsItem items are not noticed by the mouse
我正在尝试移动我的项目...唯一可行的方法是遍历我的所有项目并检查鼠标位置:
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 *widget);
paint 版本 - 它可能会被 scene()->addItem(item);
之类的东西使用 - 但那个特定的调用会使程序崩溃......从未调用过上面的 paint 方法,但我知道它是他的官方paint方法QGraphicsItem
... 这么简单的代码却这么乱
首先,尝试使用一个简单的 Item 和一个基本的 GraphicsScene 和 View。像这样:
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。
正如我们在评论中所确定的那样,问题出在 boundingRect( ) 函数返回的值上。
QGraphicsItem 的边界矩形定义项目的局部坐标。如果项目是规则的,匹配一个矩形,你只需要实现 boundingRect()。这用于碰撞检测,除其他外,检测鼠标是否在项目上。
如果您有一个非常规(矩形)对象并希望进行更细粒度的碰撞检测,那么除了 boundingRect() 之外,还要实现 shape() 函数。两者都在项目的本地坐标中。
我正在尝试移动我的项目...唯一可行的方法是遍历我的所有项目并检查鼠标位置:
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 *widget);
paint 版本 - 它可能会被 scene()->addItem(item);
之类的东西使用 - 但那个特定的调用会使程序崩溃......从未调用过上面的 paint 方法,但我知道它是他的官方paint方法QGraphicsItem
... 这么简单的代码却这么乱
首先,尝试使用一个简单的 Item 和一个基本的 GraphicsScene 和 View。像这样:
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。
正如我们在评论中所确定的那样,问题出在 boundingRect( ) 函数返回的值上。
QGraphicsItem 的边界矩形定义项目的局部坐标。如果项目是规则的,匹配一个矩形,你只需要实现 boundingRect()。这用于碰撞检测,除其他外,检测鼠标是否在项目上。
如果您有一个非常规(矩形)对象并希望进行更细粒度的碰撞检测,那么除了 boundingRect() 之外,还要实现 shape() 函数。两者都在项目的本地坐标中。