2012-07-10 115 views
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

+0

你能附加更多的代碼嗎?我猜你有一個運行得太頻繁的初始化程序,但這只是猜測。 – Smar 2012-07-11 04:18:08

+0

我添加了全班的代碼!如果你願意,我可以添加我的代碼的其他部分!擴展Component的具體類只重新定義方法drawComponent()。 – 2012-07-11 14:39:25

+0

你確定你正在使用Jambi 4.5嗎? GraphicsItemFlag.ItemSendsGeometryChanges是在Qt 4.6中引入的,所以它不會在4.5。 – Smar 2012-07-11 15:49:56

回答

0

該錯誤不再發生,原因不明!我認爲我的組件沒有正確地將它們的位置更新到graphicsscene。所以我可能會修改一些與顯示我的組件有關的代碼,並且意外地解決錯誤。如果我會找到究竟是什麼原因造成這個錯誤,我會更新這篇文章!

相關問題