QT does not know how to perform a transition between the start QBrush
and the end QBrush
. QBrush
has more properties than just color, you could envisage an animation where the color stays the same and only the pattern changes. Thus there is no default support for this kind of transition.
As @fscibe hinted, you can write your own method to perform a transition in which you specify how you want to transition from one QBrush
to the other.
Crude example:
QVariant myColorInterpolator(const QBrush &start, const QBrush &end, qreal progress)
{
QColor cstart = start.color();
QColor cend = end.color();
int sh = cstart.hsvHue();
int eh = cend.hsvHue();
int ss = cstart.hsvSaturation();
int es = cend.hsvSaturation();
int sv = cstart.value();
int ev = cend.value();
int hr = qAbs( sh - eh );
int sr = qAbs( ss - es );
int vr = qAbs( sv - ev );
int dirh = sh > eh ? -1 : 1;
int dirs = ss > es ? -1 : 1;
int dirv = sv > ev ? -1 : 1;
return QBrush(QColor::fromHsv( sh + dirh * progress * hr,
ss + dirs * progress * sr,
sv + dirv * progress * vr), progress > 0.5 ? Qt::SolidPattern : Qt::Dense6Pattern );
}
this performs a transition in the colors but also changes the pattern halfway through the transition.
Then here would be a dummy application of this transition in your code:
int main(int argc, char** argv)
{
QApplication app(argc,argv);
QGraphicsView *view = new QGraphicsView;
QGraphicsScene *scene = new QGraphicsScene;
QDialog *dialog = new QDialog;
QGridLayout *layout = new QGridLayout;
layout->addWidget(view);
view->setScene(scene);
scene->setSceneRect(-500,-500,1000,1000);
dialog->setLayout(layout);
dialog->show();
Cell *selectedCell = new Cell;
scene->addItem(selectedCell);
qRegisterAnimationInterpolator<QBrush>(myColorInterpolator);
QPropertyAnimation* animation = new QPropertyAnimation(selectedCell, "brush");
animation->setDuration(10000);
animation->setStartValue(QBrush(Qt::blue));
animation->setEndValue(QBrush(Qt::red));
animation->start();
return app.exec();
}
Obviously this is dummy example and for the color change it is re-inventing the wheel as you see from fscibe answer, but this is to show you that defining your own transition method for e.g. QBrush
you can do more than simply change the color.