PDA

View Full Version : QGraphicsObject color change animation



mvbhavsar
29th August 2011, 08:08
I have created QGraphicsObject which has multiple rectangles attached to each other. All these rects has default color as orange. This is working correctly. Now I want to change color of one particular rect after certain condition is met. I am not sure how animation will take place to change color of one rect.

I am attaching my implemention for your reference.


Thanks

Manish

wysota
29th August 2011, 08:19
What's exactly the problem?

mvbhavsar
29th August 2011, 08:40
suppose using attached object, I made an object like this.


arrayitem *ai = new arrayitem(QStringList() << "55" << "77",QFont("Calibri",20)); // This will create 2 boxes containing text 55 and 77 having background orange
for(int i=0; i<20; i++) {
if (i==10) {
// want to change background color of 55 box to green. what changes are required in attached class.
}
}

wysota
29th August 2011, 09:09
You need to provide a method in the item class for setting the color (and scheduling an update) and the color needs to be taken into consideration when painting the item.

However you will have to get rid of the for loop, it doesn't make sense.

mvbhavsar
29th August 2011, 10:31
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:
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


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



#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


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






Regards
Manish

mvbhavsar
29th August 2011, 12:48
Any suggession on this

wysota
29th August 2011, 16:50
Can you provide a compilable example reproducing the problem? The one you posted is incomplete.

mvbhavsar
30th August 2011, 05:38
Problem is resolved.

Thanks you.


Manish

akash
30th August 2011, 13:34
Hi,

Could you please provide us the complete working code of this.

Thanks,
Akash