/*
* myAction.cpp
*
* Created on: May 10, 2010
* Author: Dou NingBo
* E-mail:jason-dou@hotmail.com
* Notice: when new this object, it's parent should be inherit by QMenu
*/
#include "myAction.h"
#include "myActionWidget.h"
#include <QEvent>
MyAction::MyAction(QObject*parent,QString pressimagename,QString releaseimagename,int w,int h)
:QWidgetAction(parent)
{
iImageNameWhenPress = pressimagename;
iImageNameWhenRelease = releaseimagename;
iActionwidget = new MyActionWidget(NULL,w,h,releaseimagename);
setCheckable(true);
iActionwidget->setAttribute(Qt::WA_Hover);
iActionwidget->installEventFilter(this);
}
MyAction::~MyAction()
{
}
QWidget* MyAction::createWidget(QWidget*parent)
{
if( parent->inherits("QMenu"))
{
iActionwidget->setParent(parent);
return iActionwidget;
}
return 0;
}
bool MyAction::eventFilter(QObject *obj, QEvent *aevent)
{
if (obj == iActionwidget)
{
if (aevent->type() == QEvent::MouseButtonPress)
{
iActionwidget->setPixmap(iImageNameWhenPress);
return true;
}
else if (aevent->type() == QEvent::MouseButtonRelease)
{
iActionwidget->setPixmap(iImageNameWhenRelease);
QWidget* parent = (QWidget*)this->parent();
parent->hide();
return true;
}
else if(aevent->type() == QEvent::HoverEnter)
{
iActionwidget->setPixmap(iImageNameWhenPress);
return true;
}
else if(aevent->type() == QEvent::HoverLeave)
{
iActionwidget->setPixmap(iImageNameWhenRelease);
return true;
}
else
{
return false;
}
}
else
{
return QObject::eventFilter(obj, aevent);
}
}
评论6
最新资源