QT QMenu下拉菜单实现

QT QMenu下拉菜单实现

 

QT QMenu下拉菜单实现

  • QT QMenu下拉菜单实现
    • 样式效果
    • 代码

 

QT QMenu下拉菜单实现

点击QPushButton弹出下拉菜单

样式效果

QT QMenu下拉菜单实现_第1张图片
类似QT下拉菜单效果
QT QMenu下拉菜单实现_第2张图片

代码

QPushButton *m_pSetButton            = new QPushButton(this);
 m_pSetButton->setIcon(QIcon(":/resource/mainimage/main_set.png"));
    m_pSetButton->setStyleSheet(QString("QPushButton::menu-indicator{image:none;}"
                                        "QPushButton{border-style: none;}"
                                        "QPushButton:hover{background-color:lightgray; color: white;}"
                                        "QPushButton:pressed{background-color:rgb(85, 170, 255); border-style: inset;}"));
    m_pSetButton->setCursor(QCursor(Qt::PointingHandCursor));
    m_pSetButton->setFixedHeight(35);
    m_pSetButton->setFixedWidth(35);
    
   QMenu *setUpMenu = new QMenu();
    QAction *networkAction = new QAction(setUpMenu);
    QAction *changePwAction = new QAction(setUpMenu);
    QAction *aboutAction = new QAction(setUpMenu);
    networkAction->setIcon(QIcon(":/resource/mainimage/main_set_net.png"));
    changePwAction->setIcon(QIcon(":/resource/mainimage/main_set_password.png"));
    aboutAction->setIcon(QIcon(":/resource/mainimage/main_set_about.png"));
    networkAction->setText(tr("  网络设置"));
    changePwAction->setText(tr("  密码修改"));
    aboutAction->setText(tr("  About RPMS..."));

    setUpMenu->addAction(networkAction);
    setUpMenu->addAction(changePwAction);
    setUpMenu->addAction(aboutAction);

    setUpMenu->setWindowFlags(setUpMenu->windowFlags() | Qt::FramelessWindowHint);
    setUpMenu->setAttribute(Qt::WA_TranslucentBackground);
    setUpMenu->setStyleSheet(" QMenu {border-radius:5px;font-family:'Arial';font-size:16px;}"
                     " QMenu::item {height:35px; width:120px;padding-left:25px;border: 1px solid none;}"
                     "QMenu::item:selected {background-color:rgb(0,120,215);\
                      padding-left:25px;border: 1px solid rgb(65,173,255);}");
    m_pSetButton->setMenu(setUpMenu);
    m_pSetButton->show();

//相关槽函数实现
    connect(networkAction,&QAction::triggered, this,&RPMSystem::slot_ShowNetWorkSetUI);
    connect(changePwAction,&QAction::triggered, this,&RPMSystem::slot_ShowChangePwUI);


-------------------------------------------------

原文:https://www.it610.com/article/1293990827789459456.htm

 
posted @ 2021-02-01 11:07  世纪强风  阅读(3100)  评论(0编辑  收藏  举报