点击QPushButton上方弹出QMenu菜单源码实现


一、效果图

在这里插入图片描述

二、源码

1、菜单列表

void InitMenuAction(void)
{
    
    
    m_pSetMenu=new QMenu(this);
   // ui->otherToolBarBtn->setStyleSheet("QPushButton::menu-indicator{image:none}"); //去掉按键箭头

    QAction *pActLight = new QAction(QStringLiteral("    灯光"), this);
    QAction *pActWiper = new QAction(QStringLiteral("    雨刷"), this);
    QAction *pActInfrared = new QAction(QStringLiteral("    红外"), this);
    QAction *pActHeating = new QAction(QStringLiteral("    加热"), this);
    QAction *pActGuards = new QAction(QStringLiteral("    看守"), this);
    QAction *pActDim = new QAction(QStringLiteral("    变倍"), this);
    QAction *pActAutoScanning = new QAction(QStringLiteral("    自动扫描"), this);

    pActLight->setCheckable(true);
    pActWiper->setCheckable(true);
    pActInfrared->setCheckable(true);
    pActHeating->setCheckable(true);
    pActGuards->setCheckable(true);
    pActDim->setCheckable(true);
    pActAutoScanning->setCheckable(true);


    m_pSetMenu->addAction(pActLight);
    m_pSetMenu->addAction(pActWiper);
    m_pSetMenu->addAction(pActInfrared);
    m_pSetMenu->addAction(pActHeating);
    m_pSetMenu->addAction(pActGuards);
    m_pSetMenu->addAction(pActDim);
    m_pSetMenu->addAction(pActAutoScanning);

    m_pSetMenu->setStyleSheet("QMenu {\
                                background-color : rgb(253,253,253);\
                                padding:5px;\
                                border-radius:15px;\
                          }\
                          QMenu::item {\
                              font-size:11pt;\
                              color: rgb(85,85,85);\
                              background-color:rgb(253,253,253);\
                              padding: 8px 25px 6px 10px;\
                              margin: 4px 1px;\
                          }\
                          QMenu::item:selected {\
                              background-color : rgb(224,238,255);\
                          }\
                          QMenu::icon:checked {\
                              background: rgb(253,253,254);\
                              position: absolute;\
                              top: 1px;\
                              right: 1px;\
                              bottom: 1px;\
                              left: 1px;\
                          }\
                          QMenu::icon:checked:selected {\
                              background-color : rgb(236,236,237);\
                              image: url(:/Images/enableaction.png);\
                          }\
                          QMenu::separator {\
                              height: 2px;\
                              background: rgb(235,235,236);\
                              margin-left: 10px;\
                              margin-right: 10px;\
                          }\
                          QMenu::indicator {\
                              width: 20px;\
                              height: 20px;\
                              left:100px;\
                          }\
                          QMenu::indicator::checked {\
                              image: url(:/Images/enableaction.png);\
                          }\
                        ");
}



2、按键使能

void Dlg::on_otherToolBarBtn_clicked()
{
    
    
     QPoint pos;
     pos.setX(0);
     int ylocl = -m_pSetMenu->sizeHint().height();
     pos.setY(ylocl);
     m_pSetMenu->exec(ui->otherToolBarBtn->mapToGlobal(pos)); //在button上头显示菜单
}


三、其他功能扩展思路

你可能会发现:每次点击选中或者取消菜单中的选项后,菜单对话框都会退出;如果要想不做菜单对话框不做退出,可连续进行多项Action的选中或取消,可重写QMenu,对QMenu中的mouseReleaseEvent(QMouseEvent *e)进行重写即可。

猜你喜欢

转载自blog.csdn.net/locahuang/article/details/121203794