今天要给我做的qt控件加个tooltip,嗯,应该不难的。不过,还是先把tooltip的
来龙去脉弄清楚先的。
tool tip的定时器:
QApplication中有一个叫作toolTipWakeUp的定时器,当鼠标移动到某个QWidget时
它便会被触发。当鼠标离此QWidget时,它便会被停止。相关代码在QApplication::notify
函数中:
// 处理QEvent::MouseMove消息的代码段中,是定时器的启动
if (e->type() == QEvent::MouseMove && mouse->buttons() == 0)
{
d->toolTipWidget = w;
d->toolTipPos = relpos;
d->toolTipGlobalPos = mouse->globalPos();
d->toolTipWakeUp.start(d->toolTipFallAsleep.isActive()?20:700, this);
}
// 定时器的关闭
// User input and window activation makes tooltips sleep
switch (e->type()) {
case QEvent::Wheel:
case QEvent::ActivationChange:
case QEvent::KeyPress:
case QEvent::KeyRelease:
case QEvent::FocusOut:
case QEvent::FocusIn:
case QEvent::MouseButtonPress:
case QEvent::MouseButtonRelease:
case QEvent::MouseButtonDblClick:
d->toolTipFallAsleep.stop();
// fall-through
case QEvent::Leave:
d->toolTipWakeUp.stop();
default:
break;
}
tool tip的事件触发
tool tip事件是由上面提到的定时器触发的。并在QApplication::Event
函数中通过响应对应的定时器来发送到指定的窗口中。
tool tip的显示
好吧,tool tip的事件已经知道是怎么发送到指定的QWidget了,现在就
看看QWiget是怎么处理ToolTip事件的,QWidget::Event()的相关代码段如下:
case QEvent::ToolTip:
if (!d->toolTip.isEmpty())
QToolTip::showText(static_cast<QHelpEvent*>(event)->globalPos(),
d->toolTip, this);
else
event->ignore();
break;
至于QToolTip自身又是怎么显示的,就不详细记述了。其实,就是显示一个QLabel
打完收工~
来龙去脉弄清楚先的。
tool tip的定时器:
QApplication中有一个叫作toolTipWakeUp的定时器,当鼠标移动到某个QWidget时
它便会被触发。当鼠标离此QWidget时,它便会被停止。相关代码在QApplication::notify
函数中:
// 处理QEvent::MouseMove消息的代码段中,是定时器的启动
if (e->type() == QEvent::MouseMove && mouse->buttons() == 0)
{
d->toolTipWidget = w;
d->toolTipPos = relpos;
d->toolTipGlobalPos = mouse->globalPos();
d->toolTipWakeUp.start(d->toolTipFallAsleep.isActive()?20:700, this);
}
// 定时器的关闭
// User input and window activation makes tooltips sleep
switch (e->type()) {
case QEvent::Wheel:
case QEvent::ActivationChange:
case QEvent::KeyPress:
case QEvent::KeyRelease:
case QEvent::FocusOut:
case QEvent::FocusIn:
case QEvent::MouseButtonPress:
case QEvent::MouseButtonRelease:
case QEvent::MouseButtonDblClick:
d->toolTipFallAsleep.stop();
// fall-through
case QEvent::Leave:
d->toolTipWakeUp.stop();
default:
break;
}
tool tip的事件触发
tool tip事件是由上面提到的定时器触发的。并在QApplication::Event
函数中通过响应对应的定时器来发送到指定的窗口中。
tool tip的显示
好吧,tool tip的事件已经知道是怎么发送到指定的QWidget了,现在就
看看QWiget是怎么处理ToolTip事件的,QWidget::Event()的相关代码段如下:
case QEvent::ToolTip:
if (!d->toolTip.isEmpty())
QToolTip::showText(static_cast<QHelpEvent*>(event)->globalPos(),
d->toolTip, this);
else
event->ignore();
break;
至于QToolTip自身又是怎么显示的,就不详细记述了。其实,就是显示一个QLabel
打完收工~