I have created a wheel shape as QGraphicsObject and wanted to move along X-Axis using QPropertyAnimation. When I move this object, it gets distorted as attached in picture. My code is as follows.
#ifndef SHAPE_H
#define SHAPE_H
#include <QGraphicsObject>
#include <QtGui>
class shape : public QGraphicsObject
{
Q_OBJECT
public:
signals:
public slots:
};
#endif // SHAPE_H
#ifndef SHAPE_H
#define SHAPE_H
#include <QGraphicsObject>
#include <QtGui>
class shape : public QGraphicsObject
{
Q_OBJECT
public:
explicit shape(QGraphicsItem *parent = 0);
QRectF boundingRect() const{ return QRectF(0,0,150,150);}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
signals:
public slots:
};
#endif // SHAPE_H
To copy to clipboard, switch view to plain text mode
#include "shape.h"
QGraphicsObject(parent)
{
}
{
painter
->setRenderHint
(QPainter::Antialiasing);
painter
->setPen
(QPen(Qt
::red,
20));
painter->setBrush(Qt::transparent);
painter->drawEllipse(0,0,100,100);
painter->drawLine(50,0,50,100);
painter->drawLine(0,50,100,50);
painter->drawEllipse(40,40,20,20);
}
#include "shape.h"
shape::shape(QGraphicsItem *parent) :
QGraphicsObject(parent)
{
}
void shape::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->setRenderHint(QPainter::Antialiasing);
painter->setPen(QPen(Qt::red,20));
painter->setBrush(Qt::transparent);
painter->drawEllipse(0,0,100,100);
painter->drawLine(50,0,50,100);
painter->drawLine(0,50,100,50);
painter->drawEllipse(40,40,20,20);
}
To copy to clipboard, switch view to plain text mode
#ifndef WHEEL_H
#define WHEEL_H
#include <QMainWindow>
#include <QtGui>
#include "shape.h"
namespace Ui {
class wheel;
}
{
Q_OBJECT
public:
explicit wheel
(QWidget *parent
= 0);
~wheel();
private:
Ui::wheel *ui;
};
#endif // WHEEL_H
#ifndef WHEEL_H
#define WHEEL_H
#include <QMainWindow>
#include <QtGui>
#include "shape.h"
namespace Ui {
class wheel;
}
class wheel : public QMainWindow
{
Q_OBJECT
public:
explicit wheel(QWidget *parent = 0);
~wheel();
private:
Ui::wheel *ui;
QGraphicsScene *scene;
};
#endif // WHEEL_H
To copy to clipboard, switch view to plain text mode
#include "wheel.h"
#include "ui_wheel.h"
ui(new Ui::wheel)
{
ui->setupUi(this);
scene->setSceneRect(0,0,1,1);
ui
->graphicsView
->setRenderHints
(QPainter::Antialiasing|QPainter
::SmoothPixmapTransform);
ui->graphicsView->setScene(scene);
shape *s = new shape;
s->setPos(100,100);
scene->addItem(s);
QPropertyAnimation *anim1 = new QPropertyAnimation(s,"pos");
anim1->setDuration(5000);
anim1
->setEndValue
(QPointF(300,
100));
anim1->start();
ui->graphicsView->update();
}
wheel::~wheel()
{
delete ui;
}
#include "wheel.h"
#include "ui_wheel.h"
wheel::wheel(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::wheel)
{
ui->setupUi(this);
scene = new QGraphicsScene;
scene->setSceneRect(0,0,1,1);
ui->graphicsView->setCacheMode(QGraphicsView::CacheBackground);
ui->graphicsView->setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform);
ui->graphicsView->setScene(scene);
shape *s = new shape;
s->setPos(100,100);
scene->addItem(s);
QPropertyAnimation *anim1 = new QPropertyAnimation(s,"pos");
anim1->setDuration(5000);
anim1->setEndValue(QPointF(300,100));
anim1->start();
ui->graphicsView->update();
}
wheel::~wheel()
{
delete ui;
}
To copy to clipboard, switch view to plain text mode
#include <QtGui/QApplication>
#include "wheel.h"
int main(int argc, char *argv[])
{
wheel w;
w.show();
return a.exec();
}
#include <QtGui/QApplication>
#include "wheel.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
wheel w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks