I use Qt jambi 4.7.1 for my project. I have made a abstract class named Component who extends QGraphicsRectItem. I've set my Component flags with the purpose to move it into a same QGraphicsScene.
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));
}
So it work, now I can move my Component in my scene with a mouse click. But, I have a bug when I move my Component it always return to its initial position before each drag. I'm sure it's just a simple thing but I've searched a long time on internet and I've found nothing.
Sorry for my bad english! :P
The bug is not occurred anymore for unknown reason! I think my components didn't update their position correctly to the graphicsscene. So I probably modify some codes in relation with the display of my components and resolve the bug by accident. If I'll find what exactly cause this bug, I will update this post!