通过鼠标事件过滤器捕获鼠标滑动状态以实现单一窗口内的多页面跳转
本帖最后由 vacancy14 于 2012-08-01 10:39:25 编辑 如标题所示,想完成此项功能,写了一个Demo,出现段错误,想请各位大虾指点一二。
#include "mainwidget.h"
#include "ui_mainwidget.h"
static QPoint pMouse;
static QPoint rMouse;
static int state = 1;
int MainWidget::iCurrentWidget = 1;
MainWidget::MainWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainWidget)
{
ui->setupUi(this);
createWidget();
insertWidget();
initConnect();
//setWidget(iCurrentWidget);
}
MainWidget::~MainWidget()
{
delete ui;
}
//初始化widget栈中的对象,安装事件过滤器
void MainWidget::createWidget()
{
w1 = new Widget_1();
w2 = new Widget_2();
w3 = new Widget_3();
w1->installEventFilter(this);
w2->installEventFilter(this);
w3->installEventFilter(this);
}
//将widget对象插入栈中,设置初始widget
void MainWidget::insertWidget()
{
ui->stackedWidget->insertWidget(1,w1);
ui->stackedWidget->insertWidget(2,w2);
ui->stackedWidget->insertWidget(3,w3);
//ui->stackedWidget->setCurrentIndex(iCurrentWidget);
ui->stackedWidget->setCurrentWidget(w1);
}
void MainWidget::initConnect()
{
connect(w1,SIGNAL(goToWidget_1()),this,SLOT(goToWidget_1()));
connect(w2,SIGNAL(goToWidget_2()),this,SLOT(goToWidget_2()));
connect(w3,SIGNAL(goToWidget_3()),this,SLOT(goToWidget_3()));
}
int MainWidget::getCurrentWidgetIndex()
{
return ui->stackedWidget->currentIndex();
}
void MainWidget::goToWidget_1()
{
ui->stackedWidget->setCurrentWidget(w1);
MainWidget::iCurrentWidget = this->getCurrentWidgetIndex();
}
void MainWidget::goToWidget_2()
{
ui->stackedWidget->setCurrentWidget(w2);
MainWidget::iCurrentWidget = this->getCurrentWidgetIndex();
}
void MainWidget::goToWidget_3()
{
ui->stackedWidget->setCurrentWidget(w3);
MainWidget::iCurrentWidget = this->getCurrentWidgetIndex();
}
void MainWidget::setWidget(int index)
{
if(index == 1)
{
goToWidget_1();
}
if(index == 2)
{
goToWidget_2();
}
if(index == 3)
{
goToWidget_3();
}
}
bool MainWidget::eventFilter(QObject *obj, QEvent *evt)
{
if (!obj->isWidgetType())
{
return false;
}
//只捕获按下鼠标,拖动鼠标,松开鼠标的事件
QEvent::Type eventType = evt->type();
if(eventType != QEvent::MouseButtonPress &&
eventType != QEvent::MouseMove &&
eventType != QEvent::MouseButtonRelease)
{
return false;
}
QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent *>(evt);
switch(state)
{
case 1:
if(mouseEvent->type() == QEvent::MouseButtonPress)
{
if(mouseEvent->buttons() == Qt::LeftButton)
{
pMouse = mouseEvent->pos(); //按下鼠标那一点的坐标
state = 2;
}
}
break;
case 2:
if(mouseEvent->type() == QEvent::MouseButtonRelease)
{
state = 1; //按下就松开,跳转回原状态
}
//捕获事件为鼠标拖动事件时,记录鼠标坐标,跳转到鼠标松开事件
else if(mouseEvent->type() == QEvent::MouseMove)
{
state = 3;
rMouse = QCursor::pos();
QWidget::mapFromGlobal(rMouse);
}
break;
case 3:
if(mouseEvent->type() == QEvent::MouseButtonRelease)
{
if(qAbs(pMouse.x()-rMouse.x()) > qAbs(pMouse.y()-rMouse.y()))
{
//跳转至上一个窗口
if(pMouse.x() > rMouse.x())
{
--iCurrentWidget;
if(iCurrentWidget < 1)
{
iCurrentWidget = NUMOFWIDGET;
}
qDebug("%d\n",iCurrentWidget);
qDebug("%d\n",pMouse.x());
//ui->stackedWidget->setCurrentIndex(iCurrentWidget);
MainWidget::setWidget(iCurrentWidget);
}
//跳转至下一个窗口
else if(pMouse.x() < rMouse.x())
{
++iCurrentWidget;
if(iCurrentWidget > NUMOFWIDGET)
{
iCurrentWidget = 1;
}
qDebug("%d\n",iCurrentWidget);
qDebug("%d\n",pMouse.x());
//ui->stackedWidget->setCurrentIndex(iCurrentWidget);
MainWidget::setWidget(iCurrentWidget);
}
}
state = 1; //跳转事件处理完毕,跳回初始状态
}
break;
default:
break;
}
return MainWidget::eventFilter(obj,evt);
}