- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试移动我的项目...唯一可行的方法是遍历我的所有项目并检查鼠标位置:
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/
我最近注意到一个隐藏的进程,它没有出现在进程列表中,但它消耗CPU时间和内存。这是怎么发生的,它是如何编写的,它在做什么,我们如何杀死该进程。 有什么方法可以取消隐藏此类隐藏的进程 最佳答案 简而言之
假设我有一个相当嵌套的 JS 对象,我需要对其进行 JSON 编码: var foo = { "totA": -1, "totB": -1, "totC": "13,052.0
我正在尝试使用 scrapy 抓取一些诗歌,我有这样的文本: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean co
这可能是常识,但我似乎无法找到有关该问题的任何信息。这是一些背景知识: 我有一些页面使用了 Bootstrap 的标签系统。在这些页面的 $(document).ready() 函数中是一些基于 UR
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我创建了一组新的 Controller ,当我尝试归档新代码时,在归档这些 Controller 时发生了这个错误: error: open /Users/owner12/Library/Develo
在 Xcode 12.5.1 中使用 IOS 14.5 部署目标遵循 UIDocumentPickerViewController 的现场 IOS 文档时,出现错误: Incorrect argume
我是一名优秀的程序员,十分优秀!