qt 图片旋转角度的问题

天下如山 2013-06-21 10:37:05
有如下需求:

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


在线等 谢谢啊。
...全文
1136 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jinan654321 2016-01-08
  • 打赏
  • 举报
回复
请问上面的工程源码怎么下载?
Gary_Cui_1st 2015-06-10
  • 打赏
  • 举报
回复
引用 3 楼 ppdayz 的回复:

#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就可以了 效果就是下图,不过下图是外面的圈圈转,里面的字不转,你只要稍微改下就可以了
前辈,请教下,你的圆圈外面的一层朦胧效果是怎么做出来的?
天下如山 2013-06-21
  • 打赏
  • 举报
回复
引用 2 楼 yanbin_1985525 的回复:
QMatrix leftmatrix; leftmatrix.rotate(90); leftArrowLabel = new QLabel(); leftArrowLabel->setPixmap(QPixmap(":/images/arrowhead.png").transformed(leftmatrix,Qt::SmoothTransformation)); 把图片向右旋转90度,画在leftArrowLabel 上,其他角度你自己旋转就好了。我们项目里边就是这么用的。
非常感谢 如果使用这个方法,图片旋转的坐标原点如何设定呢?默认好像是图片中间是吗?
ppdayz 2013-06-21
  • 打赏
  • 举报
回复

#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就可以了
效果就是下图,不过下图是外面的圈圈转,里面的字不转,你只要稍微改下就可以了

醉过方知酒烈 2013-06-21
  • 打赏
  • 举报
回复
QMatrix leftmatrix; leftmatrix.rotate(90); leftArrowLabel = new QLabel(); leftArrowLabel->setPixmap(QPixmap(":/images/arrowhead.png").transformed(leftmatrix,Qt::SmoothTransformation)); 把图片向右旋转90度,画在leftArrowLabel 上,其他角度你自己旋转就好了。我们项目里边就是这么用的。
Inhibitory 2013-06-21
  • 打赏
  • 举报
回复
图片画在QWidget上,画的时候根据要旋转的角度用QPainter::rotate()先旋转后绘制。
醉过方知酒烈 2013-06-21
  • 打赏
  • 举报
回复
引用 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 上,其他角度你自己旋转就好了。我们项目里边就是这么用的。
非常感谢 如果使用这个方法,图片旋转的坐标原点如何设定呢?默认好像是图片中间是吗? [/quote] 如果要按照其他点旋转还要坐标位移了,这个比较麻烦; 简单一点 你就直接让做一张大点的图片,比如指针:一半是指针,一半是透明,按照中心旋转也可以是这个效果。
醉过方知酒烈 2013-06-21
  • 打赏
  • 举报
回复
引用 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 上,其他角度你自己旋转就好了。我们项目里边就是这么用的。
非常感谢 如果使用这个方法,图片旋转的坐标原点如何设定呢?默认好像是图片中间是吗? [/quote] 默认就是中间,你试试就知道了

16,203

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧