当前位置:编程学习 > wap >>

qt 图片旋转角度的问题

有如下需求:

一个图片(表盘的指针之类的,美工已经画好的png图片),需要摆放在一个QVBoxLayout里面,
并且随着传入角度参数不同而变化。这个该怎么实现?


在线等 谢谢啊。
Qt 图片转动 --------------------编程问答-------------------- 图片画在QWidget上,画的时候根据要旋转的角度用QPainter::rotate()先旋转后绘制。 --------------------编程问答-------------------- QMatrix leftmatrix;
leftmatrix.rotate(90);
leftArrowLabel = new QLabel();
leftArrowLabel->setPixmap(QPixmap(":/images/arrowhead.png").transformed(leftmatrix,Qt::SmoothTransformation));

把图片向右旋转90度,画在leftArrowLabel 上,其他角度你自己旋转就好了。我们项目里边就是这么用的。 --------------------编程问答--------------------

#ifndef CYCLOPROGRESS_H
#define CYCLOPROGRESS_H

#include <QLabel>
#include <QPixmap>

class CycloProgress : public QLabel
{
    Q_OBJECT
public:
    explicit CycloProgress(QWidget *parent = 0);
    void setMinLoops(int loops = 3);
    /**
     * @brief startAnimation start the Animation
     * @param interval   unit is  milliseconds
     */
    void startAnimation(int interval = 25);
    void stopAnimation();
signals:
    void runMinLoopsEnough();
protected:
    void paintEvent(QPaintEvent *);
    void timerEvent(QTimerEvent *event);
private:
    int timerID;
    int minLoops;
    QPixmap m_pixmap;
    qreal m_rotation;
};

#endif // CYCLOPROGRESS_H



#include "cycloprogress.h"
#include <QPainter>
#include <QTimerEvent>

#include <QDebug>

CycloProgress::CycloProgress(QWidget *parent) :
    QLabel(parent), minLoops(3)
{
    m_rotation = 0.0;
    timerID = 0;
    m_pixmap = QPixmap("://qrc/cyclo.png");
}


void CycloProgress::setMinLoops(int loops)
{
    minLoops = loops;
}

void CycloProgress::startAnimation(int interval)
{
    if(timerID == 0){
        timerID = startTimer(interval);
    }
}

void CycloProgress::stopAnimation()
{
    killTimer(timerID);
    timerID = 0;
}

void CycloProgress::timerEvent(QTimerEvent * event)
{
    if(event->timerId() == timerID){
        m_rotation += 5;
        update();

        if(m_rotation > minLoops*360){
            emit runMinLoopsEnough();
        }
    }
}

void CycloProgress::paintEvent(QPaintEvent * /*event*/)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::SmoothPixmapTransform);
    painter.setRenderHint(QPainter::Antialiasing);

    QPixmap leadon_text("://qrc/leadon_e_char.png");

    QPointF center(m_pixmap.width() / qreal(2), m_pixmap.height() / qreal(2));
    painter.drawPixmap(m_pixmap.width() / qreal(2) -leadon_text.width() / 2 , m_pixmap.height() / qreal(2), leadon_text);

    painter.translate(center);
    painter.rotate(m_rotation);
    painter.translate(-center);

    painter.drawPixmap(QPointF(0, 0), m_pixmap);
}


这个label直接new出来然后加入layout就可以了
效果就是下图,不过下图是外面的圈圈转,里面的字不转,你只要稍微改下就可以了

--------------------编程问答--------------------
引用 2 楼 yanbin_1985525 的回复:
QMatrix leftmatrix;
leftmatrix.rotate(90);
leftArrowLabel = new QLabel();
leftArrowLabel->setPixmap(QPixmap(":/images/arrowhead.png").transformed(leftmatrix,Qt::SmoothTransformation));

把图片向右旋转90度,画在leftArrowLabel 上,其他角度你自己旋转就好了。我们项目里边就是这么用的。

非常感谢

如果使用这个方法,图片旋转的坐标原点如何设定呢?默认好像是图片中间是吗?
--------------------编程问答--------------------
引用 4 楼 sibiyellow 的回复:
Quote: 引用 2 楼 yanbin_1985525 的回复:

QMatrix leftmatrix;
leftmatrix.rotate(90);
leftArrowLabel = new QLabel();
leftArrowLabel->setPixmap(QPixmap(":/images/arrowhead.png").transformed(leftmatrix,Qt::SmoothTransformation));

把图片向右旋转90度,画在leftArrowLabel 上,其他角度你自己旋转就好了。我们项目里边就是这么用的。

非常感谢

如果使用这个方法,图片旋转的坐标原点如何设定呢?默认好像是图片中间是吗?


默认就是中间,你试试就知道了 --------------------编程问答--------------------
引用 4 楼 sibiyellow 的回复:
Quote: 引用 2 楼 yanbin_1985525 的回复:

QMatrix leftmatrix;
leftmatrix.rotate(90);
leftArrowLabel = new QLabel();
leftArrowLabel->setPixmap(QPixmap(":/images/arrowhead.png").transformed(leftmatrix,Qt::SmoothTransformation));

把图片向右旋转90度,画在leftArrowLabel 上,其他角度你自己旋转就好了。我们项目里边就是这么用的。

非常感谢

如果使用这个方法,图片旋转的坐标原点如何设定呢?默认好像是图片中间是吗?


如果要按照其他点旋转还要坐标位移了,这个比较麻烦;
简单一点 你就直接让做一张大点的图片,比如指针:一半是指针,一半是透明,按照中心旋转也可以是这个效果。
补充:移动开发 ,  Qt
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,