1.需求描述
2.實現方法
建構函式中設定右鍵響應策略,並連線訊號槽;
this->setContextMenuPolicy(Qt::CustomContextMenu); connect(this, &QWidget::customContextMenuRequested, this,&PlayWnd::OnRightMouseClicked);
右鍵響應函式
void PlayWnd::OnRightMouseClicked(const QPoint & point) { if (m_PlayState == PLAYING) { QMenu menu(this); CustomStyle style; menu.setStyle(&style); menu.setStyleSheet(" QMenu::item{ height:34px;width:120px; padding-left:30px;} QMenu::item:pressed { background-color: rgb(32,128,247);color:#FFFFFF }"); QIcon iconLocateOrginzation = QIcon(":/pic/Resources/pic/locateorganization.svg"); QAction *LocateOrginzationAction = menu.addAction(iconLocateOrginzation, "定位組織樹"); connect(LocateOrginzationAction, &QAction::triggered, this, [=]() {emit signalToMultiWidget("LocateOrginzation",m_channelInfo); }); if (m_playWndType == PLAY_PREVIEW) { QIcon iconSnap = QIcon(":/preview/Resources/pic/Preview/ToolIcon/icon_tool_snapshot_nor.svg"); QIcon iconRecord = QIcon(":/preview/Resources/pic/Preview/ToolIcon/icon_tool_record_nor.svg"); QIcon iconRecordSel = QIcon(":/preview/Resources/pic/Preview/ToolIcon/icon_tool_record_sel.svg"); QIcon icondigtalZoom = QIcon(":/preview/Resources/pic/Preview/ToolIcon/icon_tool_zoomin_nor.svg"); QIcon icondigtalZoomSel = QIcon(":/preview/Resources/pic/Preview/ToolIcon/icon_tool_zoomin_nor.svg"); QIcon iconPtz = QIcon(":/preview/Resources/pic/Preview/ToolIcon/icon_tool_ptz_nor.svg"); QIcon iconPtzSel = QIcon(":/preview/Resources/pic/Preview/ToolIcon/icon_tool_ptz_sel.svg"); QIcon iconRePlay = QIcon(":/preview/Resources/pic/Preview/ToolIcon/icon_playback_nor.svg"); QIcon iconRePlaySel = QIcon(":/preview/Resources/pic/Preview/ToolIcon/icon_tool_return_nor.svg"); QIcon iconTalk= QIcon(":/preview/Resources/pic/Preview/ToolIcon/icon_tool_talk_nor.svg"); QIcon iconTalkSel = QIcon(":/preview/Resources/pic/Preview/ToolIcon/icon_tool_talk_sel.svg"); QIcon iconVoice = QIcon(":/preview/Resources/pic/Preview/ToolIcon/icon_tool_voice_nor.svg"); QIcon iconVoiceSel = QIcon(":/preview/Resources/pic/Preview/ToolIcon/icon_tool_mute_nor.svg"); QAction *snapAction = menu.addAction(iconSnap, "抓圖"); connect(snapAction, &QAction::triggered, this, [=]() {ui.pBViewSnap->click(); }); QAction *actionRePlay = NULL; if (ui.pBViewInstantRePlay->isChecked()) { actionRePlay= menu.addAction(iconRePlaySel, "返回預覽"); } else { actionRePlay = menu.addAction(iconRePlay, "即時回放"); QAction *actionRecord = NULL; if (ui.pBViewRecord->isChecked()) { actionRecord = menu.addAction(iconRecordSel, "停止錄影"); } else { actionRecord = menu.addAction(iconRecord, "開始錄影"); } connect(actionRecord, &QAction::triggered, this, [=]() {ui.pBViewRecord->click(); }); QAction *actionPtz = NULL; if (ui.pBViewPtz->isChecked()) { actionPtz = menu.addAction(iconPtzSel, "停止雲臺"); } else { actionPtz = menu.addAction(iconPtz, "開始雲臺"); } connect(actionPtz, &QAction::triggered, this, [=]() {ui.pBViewPtz->click(); }); } connect(actionRePlay, &QAction::triggered, this, [=]() {ui.pBViewInstantRePlay->click(); }); QAction *actionTalk = NULL; if (ui.pBViewTalk->isChecked()) { actionTalk = menu.addAction(iconTalkSel, "停止對講"); } else { actionTalk = menu.addAction(iconTalk, "開始對講"); } connect(actionTalk, &QAction::triggered, this, [=]() {ui.pBViewTalk->click(); }); QAction *actionVoice = NULL; if (ui.pBViewVoice->isChecked()) { actionVoice = menu.addAction(iconVoiceSel, "開啟聲音"); } else { actionVoice = menu.addAction(iconVoice, "關閉聲音"); } connect(actionVoice, &QAction::triggered, this, [=]() {ui.pBViewVoice->click(); }); } else { QIcon iconSnap = QIcon(":/preview/Resources/pic/Preview/ToolIcon/icon_tool_snapshot_nor.svg"); QIcon iconVoice = QIcon(":/preview/Resources/pic/Preview/ToolIcon/icon_tool_voice_nor.svg"); QIcon iconVoiceSel = QIcon(":/preview/Resources/pic/Preview/ToolIcon/icon_tool_mute_nor.svg"); QIcon iconRecord = QIcon(":/playback/Resources/pic/Playback/ToolIcon/icon_tool_clip_nor.svg"); QIcon iconRecordSel = QIcon(":/playback/Resources/pic/Playback/ToolIcon/icon_tool_clip_sel.svg"); QIcon iconDownload = QIcon(":/playback/Resources/pic/Playback/ToolIcon/icon_tool_download_nor.svg"); QIcon iconReturnView = QIcon(":/preview/Resources/pic/Preview/ToolIcon/icon_tool_return_nor.svg"); QAction *snapAction = menu.addAction(iconSnap, "抓圖"); connect(snapAction, &QAction::triggered, this, [=]() {ui.pBBackSnap->click(); }); QAction *ReturnViewAction = menu.addAction(iconReturnView, "返回預覽"); connect(ReturnViewAction, &QAction::triggered, this, [=]() {StartPreview(m_channelInfo); }); QAction *actionRecord = NULL; if (ui.pBBackRecord->isChecked()) { actionRecord = menu.addAction(iconRecordSel, "停止錄影"); } else { actionRecord = menu.addAction(iconRecord, "開始錄影"); } connect(actionRecord, &QAction::triggered, this, [=]() {ui.pBBackRecord->click(); }); QAction *downloadAction = menu.addAction(iconDownload, "下載"); connect(downloadAction, &QAction::triggered, this, [=]() {ui.pBBackDownload->click(); }); QAction *actionVoice = NULL; if (ui.pBBackVoice->isChecked()) { actionVoice = menu.addAction(iconVoiceSel, "開啟聲音"); } else { actionVoice = menu.addAction(iconVoice, "關閉聲音"); } connect(actionVoice, &QAction::triggered, this, [=]() {ui.pBBackVoice->click(); }); } menu.exec(QCursor::pos()); } }