一、思路
使用Qt的Graphics View Framework可以非常容易实现翻转的效果。该框架中的item可以通过设置QGraphicsRotation实现沿x轴、y轴和z轴(垂直于屏幕)方向上的旋转。再配合上动画便能达到上图的效果。
二、关键代码
1.既然是基于图形视图框架,那view是必不可少的。从基类中派生出我们的视图类FlipView:
FlipView : public QGraphicsView
在这个demo中视图和场景都不是主角。场景无需做其他设置,视图也只是做了一些简单的设置即可。
FlipView::FlipView(QWidget *parent):QGraphicsView(parent)
{
//场景
m_pScne = new QGraphicsScene();
this->setScene(m_pScne);
//item
m_picture = new Picture();
m_pScne->addItem(m_picture);
//设置
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setViewportUpdateMode(FullViewportUpdate); //刷新方式
setCacheMode(CacheBackground);
setRenderHints(QPainter::Antialiasing
| QPainter::SmoothPixmapTransform
| QPainter::TextAntialiasing);
}
void FlipView::drawBackground(QPainter *painter, const QRectF &rect)
{
//重写drawBackground函数绘制视图的背景
painter->drawPixmap(rect,QPixmap(":/back.jpg"),QRectF());
}
void FlipView::resizeEvent(QResizeEvent *event)
{
//在视图大小变化时,让Item填充视图(缩放)
fitInView(m_picture, Qt::KeepAspectRatioByExpanding);
}
2.demo的主角,图片Item从QGraphicsObject中派生,其主要成员变量如下:
class Picture : public QGraphicsObject
//主要成员变量:
QList<QPixmap> m_pixmaplist; //图片列表
int m_index; //当前显示的图片的下标
QPropertyAnimation* m_roateAnimation; //m_itemRotation的动画
QParallelAnimationGroup* m_animationGroup; //并行动画组,包括m_rotateAnimation和item的缩放动画
QGraphicsRotation* m_itemRotation; //旋转
在构造函数中做设置变换:
Picture::Picture(QGraphicsItem *parent):QGraphicsObject(parent)
{
//初始化
m_pixmaplist.append(QPixmap(":/01.jpg"));
m_pixmaplist.append(QPixmap(":/02.jpg"));
m_index = 0;
m_bChanged = false;
//设置该item的变换
m_itemRotation = new QGraphicsRotation(this);
m_itemRotation->setAxis(Qt::YAxis); //沿Y轴旋转
m_itemRotation->setOrigin(QVector3D(boundingRect().width()/2,0,0)); //旋转基准位置为item的中心线
this->setTransformations(QList<QGraphicsTransform *>()<<m_itemRotation);//<--关键-->
//动画效果
m_roateAnimation = new QPropertyAnimation(m_itemRotation, "angle"); //动画与m_itemRotation绑定
m_roateAnimation->setDuration(1500);
connect(m_roateAnimation,SIGNAL(finished()),this,SLOT(animationFinished()));
connect(m_roateAnimation,SIGNAL(valueChanged(QVariant)),this,SLOT(animationValueChanged(QVariant)));
QPropertyAnimation* scaleAnimation = new QPropertyAnimation(this,"scale"); //辅助效果,让item翻转的时候先缩小再放大
scaleAnimation->setDuration(1500);
scaleAnimation->setStartValue(1);
scaleAnimation->setKeyValueAt(0.5,0.3);
scaleAnimation->setEndValue(1);
setTransformOriginPoint(this->boundingRect().center());
m_animationGroup = new QParallelAnimationGroup(this);
m_animationGroup->addAnimation(m_roateAnimation);
m_animationGroup->addAnimation(scaleAnimation);
}
在构造函数中的设置已经能够实现图片的翻转效果,为了让翻转效果更逼真,我们在旋转到90度左右(图片与屏幕垂直)时换一张图片显示,好像两张图片刚好是正反两面。
在旋转动画的槽函数中进行判断:
void Picture::animationValueChanged(const QVariant &value){
if(!m_bChanged){
if(value.toDouble() >= 90 - 2 && value.toDouble() <= 90 + 2){ //旋转角度约在90度左右时切换当前图片下标
if(++m_index >= m_pixmaplist.count()){
m_index = 0;
}
m_bChanged = true; //一次旋转动画只需要判断一次是否到90度即可。
update();
}
}
}
img_src = "https://so.gushiwen.org/RandCode.ashx" # 图片的地址# 存图片from urllib import requestrequest.urlretrieve(img_src, 'code.png')from PIL import Imageimage = Image.open('code.png')image.show()...
新建空白场景定向光源模拟太阳,场景偏红晕解决方法要解决的问题解决方法要解决的问题初学者在新建空白场景时,通常都知道用定向光源+大气雾模拟基本的天空光照效果.当通常会有个疑问,为什么整个世界偏红色,视觉效果糟糕,如下图:解决方法结果如下:...
IGBT全称绝缘栅双极晶体管,它是由BJT(双极型晶体管)和MOSFET(绝缘栅型场效应管)组成的复合全控型电压驱动式电子电力器件,即具有MOSFET的输入阻抗高、控制功率小、驱动电路简单、开关速度高的优点,又具有双极型功率晶体管的电流密度大、饱和压降低、电流处理能力强的优点。IGBT的结构IGBT在结构上类似于MOSFET,其不同点在于IGBT是在N沟道功率管MOSFET的N+基板(漏极)上加了...
设置动态IP:1、sudo gedit /etc/network/interfaces2、文件修改如下auto eth0 iface eth0 inet dhcp3、sudo /etc/init.d/networking restart设置动态IP:1、同上2、auto eth0iface eth0 inet static add
问题描述:在做resnet分类的测试时,出现了这个bug: 网上给出的原因是文件导入路径的字符太长,很明显我这个不符合这个原因。还有一个说法是绝对路径相对路径的问题,具体描述如下: 在调用tf.train.Saver#save时,如果使用的路径是绝对路径(“\”),那么保存的checkpoint里面用的就是绝对路径;如果使用...
在python中,要实现“重复、自动地执行代码”,有两种循环语句可供我们选择使用:一种是for...in...循环语句,另一种是while循环语句。for循环:for循环格式:代码示例for i in [1,2,3,4,5]: print(i)运行效果图:当然这里循环的不仅仅可以是列表,也可以是字典和字符串,不可以是整数、浮点数,如果是字典的话...
caffe版本:Caffe for crfasrnn 2017/5/11 by bittntcmake … 报错:-- Boost version: 1.66.0-- Found the following Boost libraries:-- python-- Found Doxygen: /usr/bin/doxygen (found version "1.8.11") CMa...
NullPointerException相信每个JAVA程序员都不陌生,是JAVA应用程序中最常见的异常。之前,Google Guava项目曾提出用Optional类来包装对象从而解决NullPointerException。受此影响,JDK8的类中也引入了Optional类,在新版的SpringData Jpa和Spring Redis Data中都已实现了对该方法的支持。1、Optio...
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3140 题意:最近在生物实验室工作的小T遇到了大麻烦。 由于实验室最近升级的缘故,他的分格实验皿是一个长方体,其尺寸为a*b*c。为了实验的方便,它被划分为a*b*c个单位立方体区域,每个单位立方体尺寸为1*1*1。用(i,j,k)标识一个单位立方体,1 ≤i≤a,1≤j≤b,1≤...
java.math.Math类常用的常量和方法:Math.PI 记录的圆周率Math.E记录e的常量Math.abs 求绝对值Math.sin 正弦函数 Math.asin 反正弦函数Math.cos 余弦函数 Math.acos 反余弦函数Math.tan 正切函数 Math.atan 反正切函数Math.atan2 商的反正切函数Math.toDegrees 弧度转化为角度 Math.toR...
FCKeditorFCKeditor 编辑器页FCKeditor/_samples/default.htmlFCKeditor/_samples/default.htmlFCKeditor/_samples/asp/sample01.aspFCKeditor/_samples/asp/sample02.aspFCKeditor/_samples/asp/sample03.aspFCKeditor/_samples/asp/sample04.aspfckeditor/editor/filemanager/
各位,大家好,今天兔八哥给大家带来最新最新2023水果编曲软件FL Studio 21中文版下载安装激活图文教程。我们一起先了解一些FL Studio。