我正在使用下面的代码尝试旋转指针模拟时钟的秒针,但是当他转动它时,背景的方块是固定的,似乎我无法全部旋转:< / p>
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);
指针如下所示:
现在旋转90º
答案 0 :(得分:0)
Qt模拟时钟示例:
http://qt-project.org/doc/qt-5/qtwidgets-widgets-analogclock-example.html
也许在转动QPixmap
之前,请尝试画一条线。在线到位后,绘图正确地从那里向后工作。
更新:
旋转图像的一些示例代码。
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();
}
希望有所帮助。