Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicsItem movement

QGraphicsItem movement

Scheduled Pinned Locked Moved General and Desktop
14 Posts 7 Posters 14.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rileo8
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tobias.hunger
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rileo8
        wrote on last edited by
        #3

        Now the code is highlighted....sorry!

        1 Reply Last reply
        0
        • R Offline
          R Offline
          rileo8
          wrote on last edited by
          #4

          No one found this problem while moving QGraphicsItem?

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kleimola
            wrote on last edited by
            #5

            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.

            >> Johannes <<

            1 Reply Last reply
            0
            • R Offline
              R Offline
              rileo8
              wrote on last edited by
              #6

              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!

              1 Reply Last reply
              0
              • H Offline
                H Offline
                Henrik
                wrote on last edited by
                #7

                If you want to have influence of a item moved by Qt you can catch the QGraphicsItem::ItemPositionChange in the itemChange function.
                Therefor you have to set the QGraphicsItem::ItemSendsGeometryChanges flag.

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  rileo8
                  wrote on last edited by
                  #8

                  So you mean the using QGraphicsItem::ItemIsMovable flag and the QGraphicsItem::ItemSendsGeometryChanges flag togheter with QGraphicsItem::ItemPositionChange catching...but how can i limit the movement only to one axe?

                  Thanks!

                  1 Reply Last reply
                  0
                  • H Offline
                    H Offline
                    Henrik
                    wrote on last edited by
                    #9

                    You can modify the position which should be set in ItemPositionChange. So you can set the value of the axis which shouldn't be moved to the current value. Note: the values are set after ItemPositionChange.

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      rileo8
                      wrote on last edited by
                      #10

                      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);
                       }
                      

                      }

                      */@

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        daniel.zimmermann
                        wrote on last edited by
                        #11

                        I have the same problem, but with PyQt.

                        Do you have a solution for your problem meanwhile?

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          rileo8
                          wrote on last edited by
                          #12

                          At the moment i didn't found a solution.....

                          Sound strange to me that no one has a solution for this problem but....

                          1 Reply Last reply
                          0
                          • E Offline
                            E Offline
                            egan
                            wrote on last edited by
                            #13

                            void AsportoWindow::mouseMoveEvent( QGraphicsSceneMouseEvent * event )
                            {
                            ....
                            QGraphicsItem::mouseMoveEvent(event)
                            }

                            void AsportoWindow::mouseReleaseEvent( QGraphicsSceneMouseEvent * event )
                            {
                            ....
                            QGraphicsItem::mouseReleaseEvent(event)
                            }

                            void AsportoWindow::mousePressEvent( QGraphicsSceneMouseEvent * event )
                            {
                            ....
                            QGraphicsItem::mousePressEvent(event);
                            }

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              Asperamanca
                              wrote on last edited by
                              #14

                              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?

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved