// 下面三个函数监听全局的隐藏事件, 为了保证客户端不可见的时候日历隐藏 bool CPlaybackCalendarWidget::eventFilter(QObject *watched, QEvent *eve) { if (eve->type() == QEvent::ApplicationStateChange)//状态改变 { if (qApp->applicationState() == Qt::ApplicationInactive || qApp->applicationState() == Qt::ApplicationHidden)//窗口未激活或者隐藏 { hide(); ptr_camera_ = nullptr; } } return QWidget::eventFilter(watched, eve); } void CPlaybackCalendarWidget::showEvent(QShowEvent *event)//应用程序范围的事件筛选器可能会对性能产生明显的影响,因此只有在必要时才应该安装它。所以show的时候安装,hide的时候去掉事件过滤; { qApp->installEventFilter(this); return QWidget::showEvent(event); } void CPlaybackCalendarWidget::hideEvent(QHideEvent *event) { qApp->removeEventFilter(this); ptr_camera_ = nullptr; return QWidget::hideEvent(event); } }
弹窗函数
void PlayBackCtrlBar::OnPopupCalendar() { QPoint ptPos = mapToGlobal(QPoint(0, 0)); ptPos.setX(ptPos.x() + (width() - m_pCalendar->width()) / 2); ptPos.setY(ptPos.y() - m_pCalendar->height()); m_pCalendar->move(ptPos); m_pCalendar->hide();//先隐藏再显示,否则会出现在主窗口下面; m_pCalendar->show(); m_pCalendar->setFocus(Qt::FocusReason::ActiveWindowFocusReason); m_pCalendar->activateWindow();//一定要加这个激活窗口,点击其他地方会是弹窗状态由激活状态变为未激活,然后触发 //m_pCalendar->SetCurrentDateTime(ptr_progress_bar->GetCurrentDateTime()); }
标签:Qt,qApp,ptPos,pCalendar,自动隐藏,event,弹窗 From: https://www.cnblogs.com/bclshuai/p/18470111