简体   繁体   中英

QT QGraphicsView rotation

Disclaimer: I am pretty much a beginner with QT. I've been struggling for some time to rotate a QGraphicsView (no 3D rotation) but, despite what i do, it doesn't work. I have tried:

QTransform transform;
transform.rotate(45);
ui->graphicsView->setTransform(transform);

or more simply:

ui->graphicsView->rotate(45);

These seem like very straightforward ways to do it that should work, but for some reason, whenever i run it, the QGraphicsView doesn't rotate at all. If possible, i'd like some direct and easy to understand code snippets, and/or what i'm doing wrong. EDIT: This is the code in the widget cpp file i have problems with. It should be a simple timer with an animated hourglass icon. It gets repeated every .5 seconds.

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;
    }
  }
}

You're merely decorating the QGraphicsView using the style mechanism. You could have used a plain QWidget instead, since you don't use any graphics view functionality. None of the images in the stylesheet are what the view actually displays. The image must be on the scene displayed by the view.

Set the image on a QGraphicsPixmapItem , add that item to a scene, set the scene on the view, and then the transformations will work. You can then keep replacing the pixmap in the timer handler.

Finally, you must also check the timer id in the timerEvent . I assume that you're using a QBasicTimer , say called m_timer , you'd then check as follows:

void Widget::timerEvent(QTimerEvent * ev) {
  if (ev->timerId() != m_timer.timerId()) return;
  ... // rest of the code
}

As you can see, the code that you've not included in the original question was absolutely essential! Without it, the question was wholly off-topic.

You need to implement a QGraphicsView, a QGraphicsScene and then add something that inherits from QGraphicsItem to that scene to rotate.

Here is an example that rotates a QWidget in a QGraphicsView:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM