QGraphicsItem movement
-
Hi,
how can i remove the jerky movement effect when moving a QGraphicsItem?
the paltform is ubuntu 9.10 and this is the item code:
@#include "asportowindow.h"
AsportoWindow::AsportoWindow(MainScene *p,int w,int h,MysqlConnector2
*c,Logger2 *l)
{
logger=l;
conn=c;width=w;
height=h;parent=p;
this->setParentItem(parent);
setCacheMode(QGraphicsItem::DeviceCoordinateCache);movable=false;
}void AsportoWindow::paint(QPainter *painter, const
QStyleOptionGraphicsItem *option, QWidget *)
{
painter->setPen(QPen(Qt::black, 0));
painter->setBrush(QColor(78,73,73));
painter->drawRect(0,0,width,height);
}QRectF AsportoWindow::boundingRect() const
{
return QRectF(0,0,width,height);
}QPainterPath AsportoWindow::shape() const
{
QPainterPath path;
path.addRect(0,0,width,height);
return path;
}void AsportoWindow::mousePressEvent( QGraphicsSceneMouseEvent * event )
{
startMovement=event->scenePos().x();startX=this->scenePos().x(); startY=this->scenePos().y(); movable=true; lastCurrentX=startX;
}
void AsportoWindow::mouseReleaseEvent( QGraphicsSceneMouseEvent * event )
{
endMovement=event->scenePos().x();if(abs(currentMovement)<200)
{
setPos(0,startY);
}
else
{
setPos(startX,startY);
}movable=false;
}void AsportoWindow::mouseMoveEvent( QGraphicsSceneMouseEvent * event )
{
currentMovement=event->scenePos().x();if(currentMovement<startMovement) { //movememnt is right to left int delta=startMovement-currentMovement; lastCurrentX=currentX; currentX=startX-delta; width=width+delta; setPos(currentX,startY); }
}
@Thanks!
-
Could you please mark up the code in your posting as "code"? It will be much easier to read then and get line numbers to refer to.
There is a icon to do so in the toolbar right above the text entrance area.
-
What else are you doing behind the scenes in your code during the dragged move? You seem to have sql functionality and logging, are they blocking mouse or paint events at any time? You could also try to enable QGraphicsItem::ItemIsMovable flag and let Qt move your item instead. In that case remove the setPos() from mouseMoveEvent and use the item's scenePos() to get its real current pos. You could also try optimization flags for the viewport.
-
At the moment logging and sql connection are declared but not used during movement.
If i set QGraphicsItem::ItemIsMovable it work with lesser problems but i would like to have a major control over the movement.
Which type of optimization flags could i use?
Thanks!
-
I made the changes but the issue still persist...
Jerky movement...Here is the code:
@#include "asportowindow.h"
AsportoWindow::AsportoWindow(MainScene *p,int w,int h,MysqlConnector2 *c,Logger2 *l)
{
logger=l;
conn=c;width=w;
height=h;parent=p;
this->setParentItem(parent);
setFlag(ItemIsMovable,true);
setCacheMode(DeviceCoordinateCache);
// setFlag(ItemSendsGeometryChanges);// movable=false;
}void AsportoWindow::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{
painter->setPen(QPen(Qt::black, 0));
painter->setBrush(QColor(78,73,73));
painter->drawRect(0,0,width,height);
}QRectF AsportoWindow::boundingRect() const
{
return QRectF(0,0,width,height);
}QPainterPath AsportoWindow::shape() const
{
QPainterPath path;
path.addRect(0,0,width,height);
return path;
}/*
void AsportoWindow::mousePressEvent( QGraphicsSceneMouseEvent * event )
{
startMovement=event->scenePos().x();startX=this->scenePos().x(); startY=this->scenePos().y(); movable=true; lastCurrentX=startX;
}
*/
/*
void AsportoWindow::mouseReleaseEvent( QGraphicsSceneMouseEvent * event )
{
endMovement=event->scenePos().x();if(abs(currentMovement)<200) { setPos(0,startY); } else { setPos(startX,startY); } movable=false;
}
*/
/*
void AsportoWindow::mouseMoveEvent( QGraphicsSceneMouseEvent * event )
{
currentMovement=event->scenePos().x();if(currentMovement<startMovement) { //movememnt is right to left int delta=startMovement-currentMovement; lastCurrentX=currentX; currentX=startX-delta; width=width+delta; setPos(currentX,startY); }
}
*/@
-
I have the same problem, but with PyQt.
Do you have a solution for your problem meanwhile?
-
void AsportoWindow::mouseMoveEvent( QGraphicsSceneMouseEvent * event )
{
....
QGraphicsItem::mouseMoveEvent(event)
}void AsportoWindow::mouseReleaseEvent( QGraphicsSceneMouseEvent * event )
{
....
QGraphicsItem::mouseReleaseEvent(event)
}void AsportoWindow::mousePressEvent( QGraphicsSceneMouseEvent * event )
{
....
QGraphicsItem::mousePressEvent(event);
} -
For such a simple item, no cache mode should be necessary.
Can you try the 40000 chips demo example, and see if GraphicsItems in that example can be moved smoothly?