1

In this program I want to animate an instance of myqgraphicsobject whenever the user presses Qt::Key_Right. In this code I put a QPixmap as QBrush for the QRectF. I wrote the code below but it does not work. What I am I doing wrong? Thank you in advance.

#include "myqgraphicsobject.h"
MyQgraphicsObject::MyQgraphicsObject(QGraphicsItem *parent) :
QGraphicsObject(parent)
{
}

void MyQgraphicsObject::paint(QPainter *painter, )
{
    QRectF rec(0,0,50,60);
    QPixmap pi(":picture/im/super.jpg");
    pi=pi.scaled(50,60);
    painter->setBrush(QBrush(pi));
    painter->setPen(Qt::NoPen);
    painter->drawRoundedRect(rec,10,10);
}

QRectF MyQgraphicsObject::boundingRect()const
{
    return QRectF(-1,-1,70,80);
}

class MainWindow : public QMainWindow
{
   Q_OBJECT 
   public:
     explicit MainWindow(QWidget *parent = 0);
     ~MainWindow();
   private:
     QGraphicsView* view;
     QGraphicsScene* scene;
     void keyPressEvent(QKeyEvent* k);
     void keyReleaseEvent(QKeyEvent* k);
     MyQgraphicsObject* m;
     QPropertyAnimation* pr;
     QElapsedTimer* timer;
};

MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent)
{
    view=new QGraphicsView;
    scene=new QGraphicsScene;
    m=new MyQgraphicsObject;
    pr=new QPropertyAnimation(m,"pos");
    view->setScene(scene);
    view->resize(600,600);
    view->setFixedSize(600,600);
    setCentralWidget(view);
    scene->addItem(m);
    pr->setStartValue(QPoint(0,0));
    pr->setEasingCurve(QEasingCurve::InCirc);
}

void MainWindow::keyPressEvent(QKeyEvent *k)
{
    switch (k->key())
    {
      case Qt::Key_Right:
      {
          pr->start();
          //timer->start();
          QPoint p;
          p.setX(m->pos().x()+20);
          p.setY(0);
          pr->setEndValue(QPoint(p));
          pr->setDuration(1000);
          // pr->stop();
          qDebug()<<m->pos()<<endl;
          break;
      }
      default:
          break;
    }
}

void MainWindow::keyReleaseEvent(QKeyEvent *k)
{
    switch (k->key()) 
    {
      case Qt::Key_Left:
      {
          qDebug()<<"end"<<endl;
          break;
      }
      default:
          break;
    }
}
Erik
  • 2,137
  • 3
  • 25
  • 42
maryam
  • 1,437
  • 2
  • 18
  • 40

1 Answers1

1

You do not provide any details of which part doesn't work so I have taken your code and tried to fix it so that the it does what you want.

This is the keyPressEvent method after the change

void MainWindow::keyPressEvent(QKeyEvent *k)
{
  QPointF p(0,0);
  switch (k->key())
    {
    case Qt::Key_Right:
      p = m->pos();
      p.setX(p.x() + 20.0);
      pr->setEndValue(p);
      pr->setDuration(1000);
      pr->start();
      break;
    default:
      break;
    }
}

Few comments on the changes: the QPoint p is changed to QPointF p to match the return value of QGraphicsObject::pos(). The definition is moved to the outside of the case switch to avoid

jump to case label crosses initialization of QPoint p` 

errors (you can look at the accepted answer to this question for details Getting a bunch of crosses initialization error).

The other change is that in your code you first call pr->start() and only afterwards pr->setEndValue() which leads to run time errors like

QPropertyAnimation::updateState (): starting an animation without end value 

since when you start the animation is doesn't know where to end. With these changes I am able to trigger an animation on a key press without problems.

Does this help?

Community
  • 1
  • 1
Erik
  • 2,137
  • 3
  • 25
  • 42