实现思想
项目中要用到ARM,要求手势滑动能够切屏,首先我们想到使用 QGestureEvent,但使用起来比较麻烦,并且个人了解不是特别多,选择使用鼠标事件来代替,做一个通用的切屏操作。这里优先想到,鼠标点击,然后记录位置,在鼠标释放时,根据位置来判断左滑右滑。但这样做起来步骤比较繁琐,我们使用eventFilter来过滤按下与释放事件,然后在此函数中处理一切要做的事。
实现过程
普通实现其实特别简单,你只需要判断左滑右滑的方向,然后切换你想切换到的界面。但我们想让他好看一些,这里要用到 QPropertyAnimation动画实现,动画我们使用两个,一个是加载的,一个要载入的,这样看起来就有了手机那种滑动效果啦,下面直接进入我们的代码吧!
完整代码
首先在你要实现切屏的控件上调用此函数,不然不会触发EventFiter过滤事件。这里我的Page_4是我的一个子界面(qwidget) this是主界面(qwidget),根据你要实现的控件自己进行修改即可。
ui->page_4->installEventFilter(this);
m_Animationgroup = new QParallelAnimationGroup; //动画容器
m_Animationgroup这个定义在头文件里,初始化放在构造里即可,这样做的目的是,防止内存泄漏,因为我们每次滑动,都会创建两个动画,和一个动画组,每次用完就没管,这样会产生内存泄漏,所以我们每次第二次使用时调用clear函数,将以前的动画清空。在析构时记得析构动画组。
bool Form_DataManange::eventFilter(QObject *watch, QEvent *evn)
{
static int press_x; //鼠标按下时的位置
static int press_y;
static int relea_x; //鼠标释放时的位置
static int relea_y;
QMouseEvent *event = static_cast<QMouseEvent *>(evn); //将之转换为鼠标事件
if(event->type()==QEvent::MouseButtonPress) //如果鼠标按下
{
press_x = event->globalX();
press_y = event->globalY();
}
if(event->type()==QEvent::MouseButtonRelease) //如果鼠标释放
{
relea_x = event->globalX();
relea_y = event->globalY();
}
//判断滑动方向(右滑)
if((relea_x - press_x)>20 && event->type()==QEvent::MouseButtonRelease && qAbs(relea_y-press_y)<50)
{
m_Animationgroup->clear();
QImage img; //新建一个image对象
bool res = img.load("Picture/1.jpg"); //将图像资源载入对象img,注意路径,可点进图片右键复制路径
QLabel label ; //防止内存泄漏
label.setGeometry(0,0,width(),height());
label.setPixmap(QPixmap::fromImage(img)); //捕获当前界面并绘制到label上
QPropertyAnimation *animation1 = new QPropertyAnimation(&label,"geometry");
animation1->setDuration(500); //设置动画时间为1秒
animation1->setStartValue(QRect(0,0,this->width(),this->height()));
animation1->setEndValue(QRect(this->width()*2,0,this->width(),this->height()));
on_bt_NextSample_clicked(); //切换下一样品界面
QPropertyAnimation *animation2 = new QPropertyAnimation(ui->page_4,"geometry");
animation2->setDuration(500);
animation2->setStartValue(QRect(-this->width()*2,0,this->width(),this->height()));
animation2->setEndValue(QRect(0,0,this->width(),this->height()));
m_Animationgroup->addAnimation(animation1);
m_Animationgroup->addAnimation(animation2);
m_Animationgroup->start();
}
//判断滑动方向(左滑)
if((press_x - relea_x)>20 && event->type()==QEvent::MouseButtonRelease && qAbs(relea_y-press_y)<50)
{
m_Animationgroup->clear();
QImage img; //新建一个image对象
bool res = img.load("Picture/1.jpg"); //将图像资源载入对象img,注意路径,可点进图片右键复制路径
QLabel label ;//防止内存泄漏
label.setGeometry(0,0,width(),height());
label.setPixmap(QPixmap::fromImage(img)); //捕获当前界面并绘制到label上
QPropertyAnimation *animation1 = new QPropertyAnimation(&label,"geometry");
animation1->setDuration(500); //设置动画时间为1秒
animation1->setStartValue(QRect(0,0,this->width(),this->height()));
animation1->setEndValue(QRect(this->width()*2,0,this->width(),this->height()));
on_bt_UpSample_clicked(); //切换下一样品界面
QPropertyAnimation *animation2 = new QPropertyAnimation(ui->page_4,"geometry");
animation2->setDuration(500);
animation2->setStartValue(QRect(-this->width()*2,0,this->width(),this->height()));
animation2->setEndValue(QRect(0,0,this->width(),this->height()));
m_Animationgroup->addAnimation(animation1);
m_Animationgroup->addAnimation(animation2);
m_Animationgroup->start();//防止内存泄漏 使用成员变量
}
return QWidget::eventFilter(watch,evn);
}
on_bt_UpSample_clicked(); 这些是我们自己封的切换界面的函数,根据自己所需自己修改即可,经过测试,linux/arm/windows都可以使用!