0
我使用Qt jambi 4.7.1作为我的项目。我做了一个名为Component的抽象类,它扩展了QGraphicsRectItem。我已经设置了我的组件标志,目的是将它移到同一个QGraphicsScene中。Qt jambi 4.7.1 - QGraphicsItem在第二次拖动时返回到其初始位置
public abstract class Component extends QGraphicsRectItem{
public Signal1<QPoint> componentMoved = new Signal1<QPoint>();
private int id;
private int nextId;
private List<Route> routes = new ArrayList<Route>();
protected QPixmap image;
protected static QSizeF size = new QSizeF(100, 100);
public Component(int pointX1, int pointY1, int id) {
super(new QRectF(new QPointF(pointX1,pointY1),size));
this.id = id;
//this.setFlag(GraphicsItemFlag.ItemIsSelectable, true);
this.setFlag(GraphicsItemFlag.ItemIsMovable,true);
this.setFlag(GraphicsItemFlag.ItemSendsGeometryChanges, true);
this.setCacheMode(CacheMode.DeviceCoordinateCache,new QSize(1024,1024));
}
public void drawComponent(){
new QGraphicsPixmapItem(image.scaled(size.toSize()), this);
}
@Override
public void mousePressEvent(QGraphicsSceneMouseEvent e){
this.setTransformOriginPoint(e.scenePos());
}
@Override
public Object itemChange(GraphicsItemChange change, Object value){
switch (change) {
case ItemPositionHasChanged:
QPointF currentPos = this.scenePos();
componentMoved.emit(currentPos.toPoint());
break;
default:
break;
}
return super.itemChange(change, value);
}
public int getId(){
return id;
}
public int getNextId(){
return nextId;
}
public QPixmap getImage(){
return image;
}
public List<Route> getRoutes(){
return routes;
}
public QSizeF getSize(){
return size;
}
public void addRoute(Component nextComponent){
//routes.add(new Route(this, nextComponent));
}
所以它的工作,现在我可以在我的场景中用鼠标单击移动我的组件。但是,当我移动组件时,它会在每次拖动之前始终返回到其初始位置。我相信这只是一件简单的事情,但我在互联网上搜索了很长时间,而且我什么也没找到。
对不起,我的英语不好! :P
你能附加更多的代码吗?我猜你有一个运行得太频繁的初始化程序,但这只是猜测。 – Smar 2012-07-11 04:18:08
我添加了全班的代码!如果你愿意,我可以添加我的代码的其他部分!扩展Component的具体类只重新定义方法drawComponent()。 – 2012-07-11 14:39:25
你确定你正在使用Jambi 4.5吗? GraphicsItemFlag.ItemSendsGeometryChanges是在Qt 4.6中引入的,所以它不会在4.5。 – Smar 2012-07-11 15:49:56