Chinaunix首页 | 论坛 | 博客
  • 博客访问: 501290
  • 博文数量: 92
  • 博客积分: 3146
  • 博客等级: 中校
  • 技术积分: 2314
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-27 10:20
文章分类

全部博文(92)

文章存档

2014年(3)

2013年(17)

2012年(16)

2011年(22)

2010年(34)

分类: 嵌入式

2010-11-19 15:01:53

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

当鼠标划过上面的一排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();

//设置鼠标跟踪事件

this->setMouseTracking(true);

ui.label_90->setMouseTracking(true);

void ImageWidget1::page0(){

   ui.stackedWidget->setCurrentIndex(0);//表示第一个页面,以下类似

}

//鼠标点击事件,根据点击的不同的label确定发出的信号

void ImageWidget1::mousePressEvent(QMouseEvent *event){

if (event->button()==Qt:eftButton)

{

   int x=event->x();

   int y=event->y();

   if (x>x1&&xy1&&y

   {

    emit clicked1();

   }

   if (x>x2&&xy2&&y

   {

    emit clicked2();

   }

   if (x>x3&&xy3&&y

   {

    emit clicked3();

   }

   if (x>x4&&xy4&&y

   {

    emit clicked4();

   }

   if (x>x5&&xy5&&y

   {

    emit clicked5();

   }

   if (x>x6&&xy6&&y

   {

    emit clicked6();

   }

}

}

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

void ImageWidget1::mouseMoveEvent(QMouseEvent *event){

int x=event->x();

int y=event->y();

if (x>x2&&xy2&&y

{

   this->setCursor(Qt::OpenHandCursor);

   ui.label_91->setStyleSheet("background-image:url(./pic/topo_edit_down.gif)");

}

else if (x>x1&&xy1&&y

{

   this->setCursor(Qt::OpenHandCursor);

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

}

else if (x>x3&&xy3&&y

{

     this->setCursor(Qt::OpenHandCursor);

   ui.label_92->setStyleSheet("background-image:url(./pic/history_state_down.gif)");

}

else if (x>x4&&xy4&&y

{

     this->setCursor(Qt::OpenHandCursor);

   ui.label_105->setStyleSheet("background-image:url(./pic/history_log_down.gif)");

}

else if (x>x5&&xy5&&y

{

       this->setCursor(Qt::OpenHandCursor);

   ui.label_106->setStyleSheet("background-image:url(./pic/package_analyze_down.gif)");

}

else if (x>x6&&xy6&&y

{

   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 )   [virtual protected]

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

If mouse tracking is switched off, mouse move events only occur if a mouse button is pressed while the mouse is being moved. If mouse tracking is switched on, mouse move events occur even if no mouse button is pressed.

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 with QMouseEvent::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 of setToolTip() in your implementation of mouseMoveEvent().

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

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

呵呵,把他的话也一起贴出来,发现是繁体,可能是台湾同胞?MAYBE。

/////////////////////////////////////////////////////////////////////////////////// 我是传说中的分割线////////////////////////////////////////////////

我以前寫測試程序的時候遇到過這樣的問題。我跟你講下我的情況,希望對你有幫助:

我的測試程序目的是要這樣的,在一個QWidget中,放置了一個QPushButton(非常大),然後在mouseMoveEvent中定義了一個矩形塊,當鼠標移動到矩形塊内部時就變換鼠標樣式,然後使用QWidget::setMouseTracking(true)。結果是,要一直按住鼠標右鍵拖動,才響應mouseMoveEvent裏邊的語句。

最後,發現一個問題,我定義的那個矩形塊是QPushButton的勢力範圍(即這個矩形塊的最頂端的是QPushButton),使用 QPushButton::setMouseTracking(true) 之後,便可正常響應了。所以我覺得,因爲QPushButton是最頂端的物體,所以,mouseMoveEvent事件被QPushButton截獲了。

阅读(4205) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~