Question

I am using the code below to try to rotate the pointer simulating the second hand of the clock, but when he turns it cuts, the square of the background is fixed and it seems I'm not able to rotate all:

   QPixmap shipPixels(":/new/prefix1/imagem/ponteiro.png");
   QPixmap rotatePixmap(shipPixels.size());
   rotatePixmap.fill(Qt::transparent);

   QPainter p(&rotatePixmap);

   p.translate(rotatePixmap.size().width() / 2, rotatePixmap.size().height() / 2);
   p.rotate(90);
   p.translate(-rotatePixmap.size().width() / 2, -rotatePixmap.size().height() / 2);

   p.drawPixmap(0, 0, shipPixels);
   p.end();

   shipPixels = rotatePixmap;
   ui->label->setPixmap(rotatePixmap);

The pointer looks like this:

Pointer Now with it rotated 90 º With 90º

Was it helpful?

Solution

Qt Analog Clock example:

http://qt-project.org/doc/qt-5/qtwidgets-widgets-analogclock-example.html

Maybe before rotating QPixmaps, try drawing a line. After the line is in place and drawing correctly work backwards from there.

UPDATE:

Some sample code for rotating images.

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPaintEvent>
#include <QPixmap>
#include <QTime>

class Widget : public QWidget
{
    Q_OBJECT
public:
    explicit Widget(QWidget *parent = 0);

signals:

public slots:
    void paintEvent(QPaintEvent *);
private:
    QPixmap bg;
    QPixmap second_hand;
    QTime time;
};

#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include <QPainter>
#include <QTimer>
#include <QTime>

Widget::Widget(QWidget *parent) :
    QWidget(parent)
{
    time.restart();
    this->resize(256, 256);
    // both images are 256x256 in this example
    bg.load("./images/bg.png");
    second_hand.load("./images/second_hand.png");
    QTimer * t = new QTimer;
    t->setSingleShot(false);
    t->setInterval(15);
    QObject::connect(t,SIGNAL(timeout()), this, SLOT(update()));
    t->start();
}

void Widget::paintEvent(QPaintEvent * e)
{
    QPainter p(this);
    p.drawPixmap(QPoint(0,0),bg);
    qreal seconds = ((qreal)(time.elapsed() % 60000))/1000;
    p.translate(this->width()/2, this->height()/2);
    p.rotate(seconds/60*360);
    p.drawPixmap(QPoint(-this->width()/2, -this->height()/2),second_hand);
}

main.cpp

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top