Qt右键菜单实现

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());
    }
}

 

posted @ 2024-11-11 15:41  一字千金  阅读(17)  评论(0编辑  收藏  举报