Rotating QPixmap rendered in a Delegate
-
Hey all, I'm trying to develop a rotating QPixmap that is rendered in a delegate like so:
[code]
void DelegadoListView1::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
//pintor = painter;
if (option.state & QStyle::State_Selected)
{
painter->fillRect(option.rect, option.palette.color(QPalette::Highlight));
}
QString texto = index.data(Qt::DisplayRole).toString();
QRect r = option.rect.adjusted(2,2,-2,-2);
icono1.paint(painter,r,Qt::AlignVCenter|Qt::AlignLeft);
r = r.adjusted(r.height()+20,0,0,0);
painter->drawText(r.left(),r.top(),r.width(),r.height(),Qt::AlignVCenter|Qt::AlignLeft|Qt::TextWordWrap,texto,&r);}
[/code]icono1 is a QIcon, and the function takes a QModelIndex whose first index is text.
I want to rotate the QIcon, but how can I do so? I've tried tying a timer and rotating the pixmap on timeout but the image deforms,
How should I go about it? -
I've tried but it doesn't rotate the QIcon from its center, also, how can I call the paint() event from the delegate from a QTimer timeout() event?
Since I require it to move like if it was a gif, constantly spinning...EDIT: Also, the image distorts itself when rotated (not scaled, just rotated, even if in 1 degree) how can I prevent that?
-
Why a QIcon ? Why not use a QPixmap ?
-
Can you share the latest version of your code ?
As for the transformation part, did you took a look at the Coordinate Transformations doc ?
-
@SGaist
Latest version of the code is just this:
´´´
void DelegadoListView1::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
//pintor = painter;
if (option.state & QStyle::State_Selected)
{
painter->fillRect(option.rect, option.palette.color(QPalette::Highlight));
}
painter->setRenderHint(QPainter::Antialiasing);
QString texto = index.data(Qt::DisplayRole).toString();
QRect r = option.rect.adjusted(2,2,-2,-2);
if (index.data(Qt::DisplayRole).toString().contains("2"))
{QPixmap pixmap = icono1.pixmap(32,32); QMatrix matriz ; matriz = matriz.translate(pixmap.width()/2,pixmap.height()/2); matriz.rotate(angulo); pixmap = pixmap.transformed(matriz); pixmap = pixmap.scaled(64,64); QIcon icono2 = QIcon(pixmap); icono2.paint(painter,r,Qt::AlignVCenter|Qt::AlignLeft); } else icono1.paint(painter,r,Qt::AlignVCenter|Qt::AlignLeft); r = r.adjusted(r.height()+20,0,0,0); painter->drawText(r.left(),r.top(),r.width(),r.height(),Qt::AlignVCenter|Qt::AlignLeft|Qt::TextWordWrap,texto,&r);
}
QSize DelegadoListView1::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
return QSize(200,52);
}void DelegadoListView1::actualizar()
{
QPixmap pixmap = icono2.pixmap(32,32);
matriz = matriz.translate(pixmap.width()/2,pixmap.height()/2);
matriz.rotate(angulo++);
pixmap = pixmap.transformed(matriz);
pixmap = pixmap.scaled(64,64);
icono2 = QIcon(pixmap);
emit actualizarWidget();
}
´´´Right now, the Qtimer is tied to the parent widget wich upon timeout() will trigger an update(); so that it's redrawn.
Even when it is working and it does "spin", it gets seriously deformed up until the point where it's spun 360 degrees and shows itself as the first frame -
Again, why use a QIcon when you can draw the pixmap directly ?