QT QGraphicsView rotation
QT QGraphicsView rotation
声明:我基本上是QT的初学者。我一直在努力一段时间旋转QGraphicsView(没有3D旋转),但是,尽管我做了什么,它不起作用。我试过了:
QTransform transform;
transform.rotate(45);
ui->graphicsView->setTransform(transform);
或更简单:
ui->graphicsView->rotate(45);
这些看起来是非常直接的方法去做它应该工作,但出于某种原因,每当我运行它,QGraphicsView不旋转。如果可能的话,我想要一些直接和易于理解的代码片段,和/或我做错了什么。编辑:这是我有问题的小部件cpp文件中的代码。它应该是一个带有动画沙漏图标的简单计时器。每0.5秒重复一次
void Widget::timerEvent(QTimerEvent *event)
{
++timeFlag;
++timerFlag;
if (timerFlag < 115){
animateTimer = QString("":/new/100/timerFrames/timerIconFrame%1.png"").arg(timerFlag);
QPixmap pix(animateTimer);
pixmapitem.setPixmap(pix);
scene.addItem(&pixmapitem);
ui->graphicsView_2->setScene(&scene);
}
if (timerFlag >= 115 && timerFlag < 119){
//
}
if(timerFlag == 119){
ui->graphicsView_2->setStyleSheet("border-image:url(:/new/100/timerIconPix.PNG);border:0px;}");
}
if(timerFlag == 120){
timerFlag = 0;
}
if (timeFlag==2){
timeFlag = 0;
if(sec>=10){
ui->label_2->setText(QString("%1:%2").arg(min).arg(sec));
} else {
ui->label_2->setText(QString("%1:0%2").arg(min).arg(sec));
}
++sec;
if (sec == 60) {
sec = 0;
++min;
}
}
}
您只是在使用样式机制装饰QGraphicsView
。您可以使用普通的QWidget
,因为您不使用任何图形视图功能。样式表中的任何图像都不是视图实际显示的内容。该图像必须在视图显示的场景上。
将图像设置在QGraphicsPixmapItem
上,将该项目添加到场景中,将场景设置在视图中,然后转换将起作用。然后你可以继续替换定时器处理程序中的像素图。
最后,您还必须检查timerEvent
中的定时器id。我假设您使用的是QBasicTimer
,例如称为m_timer
,然后您将检查如下:
void Widget::timerEvent(QTimerEvent * ev) {
if (ev->timerId() != m_timer.timerId()) return;
... // rest of the code
}
可以看到,原始问题中没有包含的代码是绝对必要的!如果没有它,这个问题就完全跑题了。
你需要实现一个QGraphicsView,一个QGraphicsScene,然后添加一些从QGraphicsItem继承的东西到那个场景来旋转。
下面是一个在QGraphicsView中旋转QWidget的例子:
QGraphicsView* view = new QGraphicsView(parent);
QGraphicsScene* scene = new QGraphicsScene(view);
view->setScene(scene);
// Widget to rotate - important to not parent it
QWidget* widget = new QWidget();
QProxyWidget proxy_widget = scene_->addWidget(widget);
QPropertyAnimation* animation = new QPropertyAnimation(proxy_widget, "rotation");
animation->setDuration(5000);
animation->setStartValue(0);
animation->setEndValue(360);
animation->setEasingCurve(QEasingCurve::Linear);
animation->start(QAbstractAnimation::DeleteWhenStopped);