Qt中c语言case换成鼠标点击,qt中mousemoveevent和mousepressevent实现鼠标滑动换label颜色......

{

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截獲了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值