QPushButton をクリックして、QMenu メニューのソース コードをポップアップ表示します。


1. レンダリング

ここに画像の説明を挿入

二、ソースコード

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上头显示菜单
}


3. その他の機能拡張アイデア

メニューのオプションをクリックして選択またはキャンセルするたびに、メニュー ダイアログ ボックスが終了します。終了せずにメニュー ダイアログ ボックスを回避したい場合は、複数のアクションを連続して選択またはキャンセルできます。 QMenu を書き換えます。QMenu を書き換えるだけですmouseReleaseEvent(QMouseEvent *e)

おすすめ

転載: blog.csdn.net/locahuang/article/details/121203794