16,212
社区成员
发帖
与我相关
我的任务
分享
void MainWidget::onValueChanged(QVariant var) {
// qDebug()<<"var="<<var.toDouble();
item2->setTransformOriginPoint(200,200);
item2->setRotation(var.toDouble());
// graphicsView->rotate(0.5);
}
void MainWidget::onPrint() {
QVariantAnimation *animation = new QVariantAnimation();
animation->setStartValue(0);
animation->setEndValue(360);
animation->setDuration(8000);
connect(animation, SIGNAL(valueChanged(QVariant)), this, SLOT(onValueChanged(QVariant)));
animation->start();
// timeLine->start();
qDebug()<<"-- onPrint: ";
}
代码主要就是通过点击按钮触发 onPrint() 然后用 QVariantAnimation动态获取一个0~360度的渐变值,在QVariantAnimation的 onValueChanged 里面对
QGraphicsItem 类型的item2进行旋转操作,item2就是一个指针
Item2的 paint() 函数:
painter->setRenderHint(QPainter::Antialiasing, true);
painter->drawPixmap(0,0, *pixMap);
这个pixMap在Item2的构造函数里面就完成初始化了
Item2::Item2()
{
pixMap = new QPixmap(400,400);
pixMap->fill(Qt::transparent);
QPainter *painter = new QPainter(pixMap);
painter->setRenderHint(QPainter::Antialiasing);
painter->setBackgroundMode(Qt::TransparentMode);
double width = 400;
double height = 400;
double centerX = width/2;
double centerY = height/2;
double indicatorLen1 = width/40;
double indicatorLen2 = width/120;
QPainterPath path;
path.moveTo(centerX, centerY);
path.lineTo(centerX-indicatorLen1/2, centerY-indicatorLen1/2);
path.lineTo(centerX-indicatorLen2/2, 0);
path.lineTo(centerX+indicatorLen2/2,0);
path.lineTo(centerX+indicatorLen1/2, centerY-indicatorLen1/2);
path.lineTo(centerX, centerY);
painter->setPen(QPen(Qt::transparent));
painter->setBrush(QBrush(Qt::red));
painter->drawPath(path);
}