核心代码
给按钮安一个过滤器
btn1->installEventFilter(this)
鼠标移动事件
bool MainWindow::eventFilter(QObject* watched, QEvent* event)
{
//移到btn上了吗?
if (watched == btn1)
{
//是鼠标进入吗?
if (event->type() == QEvent::Enter)
{
//显示弹窗
win_ptr=new Widget();
}
//是鼠标离开吗?
if (event->type() == QEvent::Leave)
{
//删了弹窗
delete win_ptr;
}
}
//把事件还给父控件,事件继续循环
return QWidget::eventFilter(watched, event);
}