Pl. see below code code.
after using qpropertyanimation updation of qgraphicsobject is not happening properly.
#ifndef MYARRAY_H
#define MYARRAY_H
#include <QtGui>
class myarray : public QGraphicsObject
{
Q_OBJECT
public:
signals:
public slots:
private:
int x, y, wid, ht;
int cntr;
int totwid;
bool isNonArray;
};
#endif // MYARRAY_H
#ifndef MYARRAY_H
#define MYARRAY_H
#include <QtGui>
class myarray : public QGraphicsObject
{
Q_OBJECT
public:
explicit myarray(QStringList nlist, QGraphicsItem *parent = 0);
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
signals:
public slots:
private:
QStringList vlist;
QFont appFont;
int x, y, wid, ht;
int cntr;
int totwid;
bool isNonArray;
};
#endif // MYARRAY_H
To copy to clipboard, switch view to plain text mode
#include "myarray.h"
QGraphicsObject(parent)
{
vlist = nlist;
x=0;
y=0;
wid=0;
ht=0;
cntr=0;
QFont font
("Calibri",
14);
wid += fm.width(s);
qDebug() << fm.width(s);
}
totwid=wid;
ht = fm.height();
appFont = font;
}
QRectF myarray
::boundingRect() const {
return QRectF(0,
0,wid
+10,ht
+10);
}
{
Q_UNUSED(option);
Q_UNUSED(widget);
wid=0;
cntr=0;
if (cntr++==0){
x=0;
y=0;
}
qDebug() << cntr;
painter->save();
painter
->setRenderHint
(QPainter::Antialiasing,
true);
painter->setPen(Qt::red);
painter->setBrush(Qt::yellow);
wid = fm.width(s);
painter->drawRect(x,y,wid+10,ht+10);
painter->setFont(appFont);
painter
->drawText
(QRect(x
+5,y
+5,wid,ht
),Qt
::AlignHCenter|Qt
::AlignVCenter,s
);
x += wid+10;
}
painter->restore();
update();
}
#include "myarray.h"
myarray::myarray(QStringList nlist, QGraphicsItem *parent) :
QGraphicsObject(parent)
{
vlist = nlist;
x=0;
y=0;
wid=0;
ht=0;
cntr=0;
QFont font("Calibri",14);
QFontMetrics fm(font);
foreach(QString s, vlist){
wid += fm.width(s);
qDebug() << fm.width(s);
}
totwid=wid;
ht = fm.height();
appFont = font;
}
QRectF myarray::boundingRect() const
{
return QRectF(0,0,wid+10,ht+10);
}
void myarray::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
wid=0;
cntr=0;
if (cntr++==0){
x=0;
y=0;
}
qDebug() << cntr;
painter->save();
painter->setRenderHint(QPainter::Antialiasing, true);
painter->setPen(Qt::red);
painter->setBrush(Qt::yellow);
foreach(QString s, vlist){
QFontMetrics fm(appFont);
wid = fm.width(s);
painter->drawRect(x,y,wid+10,ht+10);
painter->setFont(appFont);
painter->drawText(QRect(x+5,y+5,wid,ht),Qt::AlignHCenter|Qt::AlignVCenter,s);
x += wid+10;
}
painter->restore();
update();
}
To copy to clipboard, switch view to plain text mode
#ifndef TESTGRAPHICS_H
#define TESTGRAPHICS_H
#include <QtGui>
#include "myarray.h"
namespace Ui {
class testgraphics;
}
{
Q_OBJECT
public:
explicit testgraphics
(QWidget *parent
= 0);
~testgraphics();
private slots:
void fin();
private:
Ui::testgraphics *ui;
myarray *m;
};
#endif // TESTGRAPHICS_H
#ifndef TESTGRAPHICS_H
#define TESTGRAPHICS_H
#include <QtGui>
#include "myarray.h"
namespace Ui {
class testgraphics;
}
class testgraphics : public QMainWindow
{
Q_OBJECT
public:
explicit testgraphics(QWidget *parent = 0);
~testgraphics();
private slots:
void fin();
private:
Ui::testgraphics *ui;
myarray *m;
};
#endif // TESTGRAPHICS_H
To copy to clipboard, switch view to plain text mode
#include "testgraphics.h"
#include "ui_testgraphics.h"
testgraphics
::testgraphics(QWidget *parent
) : ui(new Ui::testgraphics)
{
ui->setupUi(this);
ui->graphicsView->setScene(scene);
scene
->setSceneRect
(QRect(0,
0,
500,
500));
rect->setRect(0,0,350,350);
scene->addItem(rect);
text->setPlainText("Hello");
text->setPos(100,100);
text->setParentItem(rect);
m->setPos(150,150);
m->setParentItem(rect);
m->setOpacity(0.2);
QPropertyAnimation *anim = new QPropertyAnimation(m,"opacity");
anim->setDuration(5000);
anim->setEndValue(1.0);
connect(anim,SIGNAL(finished()),this,SLOT(fin()));
anim->start(QPropertyAnimation::DeleteWhenStopped);
}
testgraphics::~testgraphics()
{
delete ui;
}
void testgraphics::fin()
{
m->update();
}
#include "testgraphics.h"
#include "ui_testgraphics.h"
testgraphics::testgraphics(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::testgraphics)
{
ui->setupUi(this);
QGraphicsScene *scene = new QGraphicsScene;
ui->graphicsView->setScene(scene);
scene->setSceneRect(QRect(0,0,500,500));
QGraphicsRectItem *rect = new QGraphicsRectItem;
rect->setRect(0,0,350,350);
scene->addItem(rect);
QGraphicsTextItem *text = new QGraphicsTextItem;
text->setPlainText("Hello");
text->setPos(100,100);
text->setParentItem(rect);
m = new myarray(QStringList() << "152" << "116");
m->setPos(150,150);
m->setParentItem(rect);
m->setOpacity(0.2);
QPropertyAnimation *anim = new QPropertyAnimation(m,"opacity");
anim->setDuration(5000);
anim->setEndValue(1.0);
connect(anim,SIGNAL(finished()),this,SLOT(fin()));
anim->start(QPropertyAnimation::DeleteWhenStopped);
}
testgraphics::~testgraphics()
{
delete ui;
}
void testgraphics::fin()
{
m->update();
}
To copy to clipboard, switch view to plain text mode
Regards
Manish
Bookmarks