QPushButton 增加下拉菜单

头文件:

include
include
include

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 增加下拉菜单)