相信自己,只有不想做的,没有做不到的。
分类: LINUX
2014-01-16 16:14:23
当鼠标划过上面的一排Label后,鼠标形状变成手状,并且使label自动切换颜色。
很自然的会想到在一个Label上,检测鼠标的mouseMoveEvent事件,当划过label时候,切换到另一个绿色的图片。鼠标的形状可以采用QT自带的Qt::OpenHandCursor。
具体的实现为:
//往一个label添加图片
ui.label_90->setStyleSheet("background-image:url(./pic/1.jpg)");
//添加label的响应事件,clicked1是自定义的信号,page0是自定义的槽
connect(this,SIGNAL(clicked1()),this,SLOT(page0()));
//获取该个label的相关信息,
x1=ui.label_90->x();
y1=ui.label_90->y();
x1width=ui.label_90->width();
y1height=ui.label_90->height();
//设置鼠标跟踪事件
void ImageWidget1::page0(){
ui.stackedWidget->setCurrentIndex(0);//表示第一个页面,以下类似
}
//鼠标点击事件,根据点击的不同的label确定发出的信号
void ImageWidget1::mousePressEvent(QMouseEvent *event){
if (event->button()==Qt::LeftButton)
{
int x=event->x();
int y=event->y();
if (x>x1&&x
emit clicked1();
}
if (x>x2&&x
emit clicked2();
}
if (x>x3&&x
emit clicked3();
}
if (x>x4&&x
emit clicked4();
}
if (x>x5&&x
emit clicked5();
}
if (x>x6&&x
emit clicked6();
}
}
}
//下面监听鼠标滑动,并获取坐标,判断在哪个label的范围内,根据不同的label重新设置其背景图片,造成label变色的效果,并且使鼠标变成手状,具有提醒效果。如果在其余的区域,设置label为默认的图片。
void ImageWidget1::mouseMoveEvent(QMouseEvent *event){
int x=event->x();
int y=event->y();
if (x>x2&&x
this->setCursor(Qt::OpenHandCursor);
ui.label_91->setStyleSheet("background-image:url(./pic/topo_edit_down.gif)");
}
else if (x>x1&&x
this->setCursor(Qt::OpenHandCursor);
ui.label_90->setStyleSheet("background-image:url(./pic/topo_show_down.gif)");
}
else if (x>x3&&x
this->setCursor(Qt::OpenHandCursor);
ui.label_92->setStyleSheet("background-image:url(./pic/history_state_down.gif)");
}
else if (x>x4&&x
this->setCursor(Qt::OpenHandCursor);
ui.label_105->setStyleSheet("background-image:url(./pic/history_log_down.gif)");
}
else if (x>x5&&x
this->setCursor(Qt::OpenHandCursor);
ui.label_106->setStyleSheet("background-image:url(./pic/package_analyze_down.gif)");
}
else if (x>x6&&x
this->setCursor(Qt::OpenHandCursor);
ui.label_107->setStyleSheet("background-image:url(./pic/package_control_down.gif)");
}
else{
this->setCursor(Qt::ArrowCursor);
//如果鼠标停在了非上面一排label上,恢复鼠标形状,并设置label为默认图片
ui.label_90->setStyleSheet("background-image:url(./pic/1.jpg)");
ui.label_91->setStyleSheet("background-image:url(./pic/2.jpg)");
ui.label_92->setStyleSheet("background-image:url(./pic/3.jpg)");
ui.label_105->setStyleSheet("background-image:url(./pic/4.jpg)");
ui.label_106->setStyleSheet("background-image:url(./pic/5.jpg)");
ui.label_107->setStyleSheet("background-image:url(./pic/6.jpg)");
}
需要注意的有两点:
1 上面红字标注的地方,这个也是查了好多资料才找到的。
我们先看看Qt的Assistance中关于mouseMoveEvent的描述:
This event handler, for event event, can be reimplemented in a subclass to receive mouse move events for the widget.
() reports the position of the mouse cursor, relative to this widget. For press and release events, the position is usually the same as the position of the last mouse move event, but it might be different if the user's hand shakes. This is a feature of the underlying window system, not Qt.
If you want to show a tooltip immediately, while the mouse is moving (e.g., to get the mouse coordinates with() and show them as a tooltip), you must first enable mouse tracking as described above. Then, to ensure that the tooltip is updated immediately, you must call () instead of() in your implementation of mouseMoveEvent().
See also (), (), (), (),(), , and .
注意我上面红色标注的地方,说如果mouseTracking的状态关了,那么必须按着鼠标拖动才能触发。所以,必须要this->setMouseTracking(true),但如果仅仅这样,也就没什么新奇的了。我也就没必要特别在这写出来。很容易发现,仅仅设置这句是不行的,你如果想让它滑过一个控件,触发某种效果,你就会发现在滑过控件的时候依然需要按着鼠标滑动。原因就在与鼠标发出的事件被控件所截获,所以要对控件设置setMouseTracking才行。这些是参考了一个人的资料,如果没有他的话,估计我现在还没研究出来呢。
呵呵,把他的话也一起贴出来,发现是繁体,可能是台湾同胞?MAYBE。