Qt - 如何获取 QGraphicsItem 相对于场景的位置
Qt - How do I get a QGraphicsItem's position relative to scene
我有一个从 QGraphicsRectItem
派生的类的构造函数:
Tower::Tower(int x, int y, QGraphicsScene *scene) : QGraphicsRectItem(x, y, width, height)
{
QBrush brush; // color it
brush.setStyle(Qt::SolidPattern);
brush.setColor(START_COLOR);
this->setBrush(brush);
this->setAcceptHoverEvents(true);
scene->addItem(this); // add to scene
}
我有一个fire
函数的代码,该函数应在矩形的中心创建子弹:
void Tower::fire()
{
Bullet *bullet = new Bullet(scenePos().x() + width / 2, scenePos().y() + height / 2, scene());
}
这是Bullet
的代码:
Bullet::Bullet(int x, int y, QGraphicsScene *scene) : QGraphicsRectItem(x, y, width, height)
{
QBrush brush; // color it
brush.setStyle(Qt::SolidPattern);
brush.setColor(color);
this->setBrush(brush);
scene->addItem(this);
}
当功能运行时,它会在场景开头创建子弹。
我该怎么办来解决这个问题?
我检查了其他问题,但他们的答案对我不起作用。
在QGraphicsRectItem
中,位置不在矩形的左上角,而是默认项目位置(0,0)。
如果您想要中心的场景位置,则可以做类似的事情:
QPointF Tower::sceneCenter()
{
return mapToScene(rect().center());
}
相关文章:
- 从udp接收帧对于人脸识别来说太慢
- 对于C++中使用智能指针的指针算术限制,有没有一种变通方法
- 对于set上的循环-获取next元素迭代器
- C++核心准则 C35 对于接口类"A base class destructor should be either public and virtual, or protected and nonv
- unique_ptr相对于shared_ptr的优势是什么
- int 相对于C++中主代码的目的
- SourceAddress 是否相对于 MmCopyVirtualMemory 中的 SourceProcess?
- 相对于类statics的基元全局的生存期
- 实现现有的网络接口,以相对于C 11中的Endianess定义位字段
- 如何添加相对于QTABBAR中标签的位置的小部件
- 相对于 3D 应用程序中的 Windows 消息循环的计时错误
- §12.3.2 [class.conv.fct]/1 in C++1z 相对于 C++14 发生了重大变化。有意义吗?
- 相对于 cpp 不工作目录的路径
- 使用原生C++Qt相对于PyQt有什么优势?
- 打开相对于chromium.exe的文件
- msvcr100相对于msvcrt的优势
- at()函数相对于get(ch)的必要性
- unique_ptr相对于auto_ptr的优点
- c++中std::thread相对于pthread的优势
- 哈希,相对于x,y位置的多边形