#ifndef MOUSEBASE_H
#define MOUSEBASE_H
#include <QtGui>
#include <QMouseEvent>
class CMouseBase : public QFrame
{
Q_OBJECT
public:
CMouseBase(QWidget *parent = 0);
~CMouseBase();
signals:
void signalsMousePosition(int ix,int iy);
protected :
void mouseMoveEvent ( QMouseEvent * e );
};
#endif
2.实现文件如下:MouseBase.cpp
#include "MouseBase.h"
CMouseBase::CMouseBase (QWidget *parent) : QFrame(parent)
{
this->setMouseTracking ( true); //设置此窗体是否跟踪鼠标
}
CMouseBase::~CMouseBase ()
{
}
void CMouseBase::mouseMoveEvent ( QMouseEvent * e )
{
emit signalsMousePosition(e->x(),e->y());
}
3.新建UI文件,ui中包括 主窗体,左窗体和右窗体类型为CMouseBase,按钮。再把左右窗体放入网格布局管理器中。
4.绑定左右窗体的鼠标信号
connect(ui.LeftFrame,SIGNAL(signalsMousePosition(int,int)),this,SLOT(slotsMousePosition(int,int)));
connect(ui.RightFrame,SIGNAL(signalsMousePosition(int,int)),this,SLOT(slotsMousePosition(int,int)));
5.信号处理函数如下:
void test::slotsMousePosition(int ix,int iy)
{
CMouseBase *pWidow = qobject_cast< CMouseBase *>(sender());
if(pWidow == ui.RightFrame)
{
//这里传入的坐标是当前对象的相对坐标
int iYTop=(ui.RightFrame->height()- ui.pushButton->height())/2;
int iYBottom=(ui.RightFrame->height()+ ui.pushButton->height())/2;
if((ix<10) &&(ix>0)&&(iy>iYTop)&&(iy<iYBottom))
{
ui.pushButton->show();
}
else
{
ui.pushButton->hide();
}
}
else
{
ui.pushButton->hide();
}
}
qt版本:4.8.6,开发环境:vs2010
本着开源的精神,公布源代码和可执行程序,下载地址:
http://yunpan.cn/QaRTy7Au6ByqK (提取码:b7a2)
注:
欢迎交流技术,转载请注明出处并保持作品的完整性。
作者:
程序人生 原文:
http://blog.csdn.net/hiwubihe/article/details/38678305