item不返回自定义qGraphicsItem [英] itemAt not returning custom qGraphicsItem
问题描述
我有一个qGraphicsScene的自定义实现和一个自定义的qGraphicsItem,我点击,但itemAt函数从不返回一个值,即使我相当确定我点击项目。
void VScene :: mouseReleaseEvent(QGraphicsSceneMouseEvent * event)
{
if((VMouseClick)&& pos()== vLastPoint)){
QGraphicsItem * mod = itemAt(event-> pos(),QTransform());
if(mod){//永远不会返回true
// ...
}
}
}
为了清楚起见,该模块添加了以下代码:
void VScene :: addModule(QString modName,QPointF dropPos)
{
VModule * module = new VModule();
addItem(module);
// QPointF值来自mainWindow中的一个事件,坐标映射到我的场景。
module-> setPos(dropPos);
}
...这里是我写的自定义qGraphicsItem。 p>
VModule.h:
class VModule:public QObject,public QGraphicsItem
{
public:
explicit VModule();
QRectF boundingRect()const;
void paint(QPainter * painter,const QStyleOptionGraphicsItem * option,QWidget * widget);
private:
qreal w;
qreal h;
int xAddr;
int yAddr;
QPolygonF baseShape;
}
VModule.cpp:
VModule :: VModule()
{
w = 80;
h = 80;
xAddr = w / 2
yAddr = h / 2;
//使用数字创建多个多边形
QVector< QPointF> basePoints = {QPointF(0.0,0.0),
QPointF(xAddr,yAddr),
QPointF(0.0,yAddr * 2),
QPointF(-xAddr,yAddr)};
baseShape = QPolygonF(basePoints);
}
QRectF VModule :: boundingRect()const
{
return QRectF(-xAddr,0,w,h);
}
void VModule :: paint(QPainter * painter,const QStypeOptionGraphicsItem * option,QWidget * widget)
{
//画笔等设置
// ...
painter-> drawPolygon(baseShape,qt :: OddEvenFill);
//其他多边形的绘制方式与上述相同
}
我的实现有任何问题吗?有没有我缺少的东西?感谢您提供任何帮助。
您正在以项目坐标而不是场景坐标查询场景。使用:
...
QGraphicsItem * mod = itemAt(event-> scenePos());
...
I have a custom implementation of qGraphicsScene and a custom qGraphicsItem that I click on, but the itemAt function never returns a value, even though I am fairly certain that I'm clicking on the item.
void VScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if ((vMouseClick) && (event->pos() == vLastPoint)) {
QGraphicsItem *mod = itemAt(event->pos(), QTransform());
if (mod) { // Never returns true
// ...
}
}
}
For clarity, the module is added in the following code:
void VScene::addModule(QString modName, QPointF dropPos)
{
VModule *module = new VModule();
addItem(module);
// the QPointF value comes from an event in mainWindow, the coordinate is mapped to my scene.
module->setPos(dropPos);
}
... and here is the custom qGraphicsItem that I have written.
VModule.h:
class VModule : public QObject, public QGraphicsItem
{
public:
explicit VModule();
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
private:
qreal w;
qreal h;
int xAddr;
int yAddr;
QPolygonF baseShape;
}
VModule.cpp:
VModule::VModule()
{
w = 80;
h = 80;
xAddr = w / 2;
yAddr = h / 2;
// Use the numbers to create a number of polygons
QVector<QPointF> basePoints = { QPointF(0.0, 0.0),
QPointF(xAddr, yAddr),
QPointF(0.0, yAddr * 2),
QPointF(-xAddr, yAddr) };
baseShape = QPolygonF(basePoints);
}
QRectF VModule::boundingRect() const
{
return QRectF(-xAddr, 0, w, h);
}
void VModule::paint(QPainter *painter, const QStypeOptionGraphicsItem *option, QWidget *widget)
{
// brushes and so on are set
// ...
painter->drawPolygon(baseShape, qt::OddEvenFill);
// there are other polygons are drawn in the same way as above
}
Are there any problems with my implementation? Is there something I am missing? Thanks in advance for any help.
You are querying the scene in item coordinates instead of scene coordinates. Use:
...
QGraphicsItem *mod = itemAt(event->scenePos());
...
这篇关于item不返回自定义qGraphicsItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!