Qt中mouseMoveEvent和mousePressEvent实现鼠标滑动换label颜色

来源:互联网 发布:单片机协议栈开发 编辑:程序博客网 时间:2024/06/10 02:32

这个搞的时间比较长,最后实现了,却发现因为一个小的失误悲剧的我差点内牛满面。。。

当鼠标划过上面的一排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<x1+x1width&&y>y1&&y<y1+y1height)
   {
    emit clicked1();
   }
   if (x>x2&&x<x2+x2width&&y>y2&&y<y2+y2height)
   {
    emit clicked2();
   }
   if (x>x3&&x<x3+x3width&&y>y3&&y<y3+y3height)
   {
    emit clicked3();
   }
   if (x>x4&&x<x4+x4width&&y>y4&&y<y4+y4height)
   {
    emit clicked4();
   }
   if (x>x5&&x<x5+x5width&&y>y5&&y<y5+y5height)
   {
    emit clicked5();
   }
   if (x>x6&&x<x6+x6width&&y>y6&&y<y6+y6height)
   {
    emit clicked6();
   }
}
}

//下面监听鼠标滑动,并获取坐标,判断在哪个label的范围内,根据不同的label重新设置其背景图片,造成label变色的效果,并且使鼠标变成手状,具有提醒效果。如果在其余的区域,设置label为默认的图片。

void ImageWidget1::mouseMoveEvent(QMouseEvent *event){
int x=event->x();
int y=event->y();

if (x>x2&&x<x2+x2width&&y>y2&&y<y2+y2height)
{
   this->setCursor(Qt::OpenHandCursor);
   ui.label_91->setStyleSheet("background-image:url(./pic/topo_edit_down.gif)");
}
else if (x>x1&&x<x1+x1width&&y>y1&&y<y1+y1height)
{
   this->setCursor(Qt::OpenHandCursor);

   ui.label_90->setStyleSheet("background-image:url(./pic/topo_show_down.gif)");
}

else if (x>x3&&x<x3+x3width&&y>y3&&y<y3+y3height)
{
     this->setCursor(Qt::OpenHandCursor);
   ui.label_92->setStyleSheet("background-image:url(./pic/history_state_down.gif)");
}
else if (x>x4&&x<x4+x4width&&y>y4&&y<y4+y4height)
{
     this->setCursor(Qt::OpenHandCursor);
   ui.label_105->setStyleSheet("background-image:url(./pic/history_log_down.gif)");
}
else if (x>x5&&x<x5+x5width&&y>y5&&y<y5+y5height)
{
       this->setCursor(Qt::OpenHandCursor);
   ui.label_106->setStyleSheet("background-image:url(./pic/package_analyze_down.gif)");
}
else if (x>x6&&x<x6+x6width&&y>y6&&y<y6+y6height)
{
   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的描述:

void QWidget::mouseMoveEvent ( QMouseEvent * event )  

This event handler, for event event, can be reimplemented in a subclass to receive mouse move events for the widget.

QMouseEvent::pos() 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 withQMouseEvent::pos() 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 QToolTip::showText() instead ofsetToolTip() in your implementation of mouseMoveEvent().

See also setMouseTracking(), mousePressEvent(), mouseReleaseEvent(), mouseDoubleClickEvent(), event(),QMouseEvent, and Scribble Example.

注意我上面红色标注的地方,说如果mouseTracking的状态关了,那么必须按着鼠标拖动才能触发。所以,必须要this->setMouseTracking(true),但如果仅仅这样,也就没什么新奇的了。我也就没必要特别在这写出来。很容易发现,仅仅设置这句是不行的,你如果想让它滑过一个控件,触发某种效果,你就会发现在滑过控件的时候依然需要按着鼠标滑动。原因就在与鼠标发出的事件被控件所截获,所以要对控件设置setMouseTracking才行。这些是参考了一个人的资料,如果没有他的话,估计我现在还没研究出来呢。

0 0
原创粉丝点击