利用Qt视图场景类编写的可在二维、三维控件旋转、放大、缩小的例子

本文介绍了一个Qt应用程序中的二维和三维旋转实现方法。通过控制旋转方向和角度,可以对QWidget进行顺时针或逆时针旋转,并支持沿不同轴的三维旋转。此外还提供了放大和缩小的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果如下: 

代码如下,其中二维旋转也可以用三维旋转的方式来做。

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private:
    Ui::Widget *ui;
	//初始化界面格式
    void InitDlg();

public:
	//二维旋转  direction:1 -顺时针  0-逆时针  angle:(-360, 360)
    void Rotate2D(bool direction, float angle);

    //这里的x轴,y轴均垂直于屏幕,而不是垂直于对话框
    //三维旋转  direction:1 -顺时针  0-逆时针  angle:(-360, 360)
    void Rotate3D_x(bool direction, float angle);

    void Rotate3D_y(bool direction, float angle);
private slots:
    void on_rotate2D_clicked();
    void on_rotate3D_clicked();
    void on_rotate2D_2_clicked();
    void on_rotate3D_2_clicked();
    void on_rotate3D_3_clicked();
    void on_rotate3D_4_clicked();

    void on_fangda_clicked();

    void on_suoxiao_clicked();

public:
    void paintEvent( QPaintEvent * e );
};

#endif // WIDGET_H

 

#include "widget.h"
#include "ui_widget.h"

#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsProxyWidget>
#include <QGraphicsRotation>
#include <QPixmap>
#include <QDebug>

QGraphicsScene *scene;
QGraphicsProxyWidget *widget;
QGraphicsView *view;
QGraphicsRotation *rotate;

QPixmap *map;

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    //设置widget透明度
    //setWindowOpacity(1);

    //创建scene
    scene = new QGraphicsScene;

    //将widget嵌入到scene,返回指向指向代理的指针
    widget = scene->addWidget(this);

    //设置中心为2D旋转中心
    widget->setTransformOriginPoint(this->width()/2, this->height()/2);

    //创建view
    view = new QGraphicsView;

    //创建旋转对象
    rotate = new QGraphicsRotation(widget);

    //关联scene
    view->setScene(scene);

    //初始化界面效果
    InitDlg();

    //加载图片
    map = new QPixmap;
    map->load("picture.png");

}


void Widget::InitDlg()
{
    //设置view大小
    view->resize(1000, 1000);

    //设置view客户区背景透明
    view->setStyleSheet("background:transparent;border:0px");

    //设置view无边框
    view->setWindowFlags(Qt::FramelessWindowHint);

    //不显示view的滑动条
    //view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    //view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    //设置view窗口透明
    view->setAttribute(Qt::WA_TranslucentBackground, true);

    //显示view
    view->show();
}


void Widget::Rotate2D(bool direction, float angle)
{
    static int origAngle = 0;

    if(direction)
        origAngle -= angle;
    else
        origAngle += angle;

    //旋转QWidget
    widget->setRotation(origAngle);

    //也可以用实现三维的方式实现二维
//     rotate->setAxis(Qt::ZAxis);
//     rotate->setAngle(origAngle);
//     rotate->setOrigin(QVector3D(widget->boundingRect().height()/2.0, widget->boundingRect().width()/2.0, 0));
//     //旋转
//     widget->setTransformations(QList<QGraphicsTransform *>()<<rotate);

    //平滑边缘
    view->setRenderHint(QPainter::SmoothPixmapTransform, true);
}

void Widget::Rotate3D_x(bool direction, float angle)
{
    static int origAngle = 0;

    if(direction)
        origAngle -= angle;
    else
        origAngle += angle;

    rotate->setAxis(Qt::XAxis);
    //设置旋转中心
    rotate->setOrigin(QVector3D(widget->boundingRect().width()/2.0, widget->boundingRect().height()/2.0, 0));
    //设置旋转角度
    rotate->setAngle(origAngle);

    //旋转
    widget->setTransformations(QList<QGraphicsTransform *>()<<rotate);
    //平滑边缘
    view->setRenderHint(QPainter::SmoothPixmapTransform, true);
}


void Widget::Rotate3D_y(bool direction, float angle)
{
    static int origAngle = 0;

    if(direction)
        origAngle -= angle;
    else
        origAngle += angle;

    rotate->setAxis(Qt::YAxis);
    //设置旋转中心
    rotate->setOrigin(QVector3D(widget->boundingRect().width()/2.0, widget->boundingRect().height()/2.0, 0));
    //设置旋转角度
    rotate->setAngle(origAngle);
    //旋转
    widget->setTransformations(QList<QGraphicsTransform *>()<<rotate);
    //平滑边缘
    view->setRenderHint(QPainter::SmoothPixmapTransform, true);
}

void Widget::paintEvent( QPaintEvent * e )
{
    QPainter painter(this);
    painter.drawPixmap(0, 0, 400, 300, *map);
}

void Widget::on_rotate2D_clicked()
{
    Rotate2D(0, 10);
}

void Widget::on_rotate2D_2_clicked()
{
    Rotate2D(1, 10);
}

void Widget::on_rotate3D_clicked()
{
    Rotate3D_x(1, 10);
}

void Widget::on_rotate3D_2_clicked()
{
    Rotate3D_x(0, 10);
}

void Widget::on_rotate3D_3_clicked()
{
    Rotate3D_y(1, 10);
}

void Widget::on_rotate3D_4_clicked()
{
    Rotate3D_y(0, 10);
}

Widget::~Widget()
{
	delete ui;
}

//放大
void Widget::on_fangda_clicked()
{
    static int a = 1;
    qreal s = pow(1.1, a+0.5);
    view->scale(s, s);
}

//缩小
void Widget::on_suoxiao_clicked()
{
	static int a = 1;
	qreal s = pow(1/1.1, a+0.5);
	view->scale(s, s);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值