美文网首页
QPushButton 增加下拉菜单

QPushButton 增加下拉菜单

作者: 猩猩隊長 | 来源:发表于2018-10-25 12:42 被阅读0次

    头文件:

    include <QPushButton>
    include <QMenu>
    include <QAction>

    main.cpp:

        QPushButton* Btn = new QPushButton("Hello QPushButton!", &w);
    
        QMenu* menu= new QMenu(); 
        QAction* m_addAction = new QAction(menu);
        QAction* m_delAction = new QAction(menu);
        QAction* m_cpyAction = new QAction(menu);
    
        m_addAction->setText("添加");
        m_delAction->setText("删除");
        m_cpyAction->setText("编辑");
    
        menu->addAction(m_addAction);
        menu->addAction(m_delAction);
        menu->addAction(m_cpyAction);
    
    
        QMenu* menu_sec = new QMenu();
        QAction* m_secActionCut = new QAction(menu_sec);
        QAction* m_secActionCpy = new QAction(menu_sec);
        QAction* m_secActionPas = new QAction(menu_sec);
    
        m_secActionCut->setText("剪切");
        m_secActionCpy->setText("复制");
        m_secActionPas->setText("粘贴");
    
        menu_sec->addAction(m_secActionCut);
        menu_sec->addAction(m_secActionCpy);
        menu_sec->addAction(m_secActionPas);
    
         //将menu_sec菜单对象添加到menu菜单对象的m_cpyAction对象中
        m_cpyAction->setMenu(menu_sec); 
    
        Btn->setMenu(menu);
        Btn->resize(150 ,22);
        Btn->show();
    

    效果图:

    x.jpg

    相关文章

      网友评论

          本文标题:QPushButton 增加下拉菜单

          本文链接:https://www.haomeiwen.com/subject/buvqtqtx.html