QMenu的使用

添加分割线样式 

QMenu
{
        color:rgb(123,123,123);
        background-color:#052331;
        border:none;
}
QMenu::item
{
        color:rgb(123,123,123);
        background-color:rgb(75,171,121);
        width:50px;
        height:35px;
        padding-left:20px;
        border:2px solid rgb(60,135,230);
}
QMenu::item:selected
{
        color:rgb(123,123,123);
        background-color:#1B1351;
}
QMenu::separator
{
        height:1px;
        background-color:rgba(123,123,123,1);
        margin-left:6px;
        margin-right:6px;
}


QMenu::indicator:unchecked
{
    border:2px solid rgb(125,125,125);
}

 

/* 不可用(操作)选项颜色灰色*/

QMenu::item:!enabled   

{
color:rgb(128,136,145);

background-color:rgba(255,255,255,1);

}

 多级菜单

    QMenu *menu = new QMenu();   
    menu->setMinimumWidth(150);//指定菜单的宽度为150 
 
    SystemMenu = new QMenu(this);
    SystemMenu->setTitle(tr("帮助"));
    QIcon icon(":/images/help1.png");
    SystemMenu->setIcon(icon);
 
    LogMgtMenu = new QMenu(this);
    LogMgtMenu->setTitle(tr("日志管理"));
    QIcon icon2(":/images/logMgn.png");
    LogMgtMenu->setIcon(icon2);
 
    SettingMenu = new QMenu(this);
    SettingMenu->setTitle(tr("设置"));
    QIcon icon4(":/images/setting.png");
    SettingMenu->setIcon(icon4);
 
    menu->addMenu(LogMgtMenu);
    menu->addMenu(SettingMenu);
    menu->addMenu(SystemMenu);
 
    HandBookAction = new QAction(this);
    HandBookAction->setText(tr("IPS 帮助"));
    HandBookAction->setShortcut(Qt::Key_F1);
    connect(HandBookAction,SIGNAL(triggered()),this,SIGNAL(HandBook()));
    QIcon icon(":/images/help1.png");
    HandBookAction->setIcon(icon);
 
    AboutAction = new QAction(this);
    AboutAction->setText(tr("关于"));
    connect(AboutAction,SIGNAL(triggered()),this,SIGNAL(ShowAbout()));
    QIcon icon1(":/images/about.png");
    AboutAction->setIcon(icon1);
 
    ExitAction = new QAction(this);
    ExitAction->setText(tr("退出"));
    ExitAction->setShortcut(Qt::CTRL|Qt::Key_Q);
    connect(ExitAction, SIGNAL(triggered()), this, SIGNAL(Exit()));
    QIcon icon2(":/images/exit.png");
    ExitAction->setIcon(icon2);
 
    SystemMenu->addAction(HandBookAction);
    SystemMenu->addAction(AboutAction);
 
    menu->addAction(ExitAction);

Qt按钮添加菜单

 //构造函数调用,创建菜单
 void Widget::initMenu()
 {
     menu = new QMenu(this);
     menu->setWindowFlags(menu->windowFlags() | Qt::FramelessWindowHint);
     //menu->setAttribute(Qt::WA_TranslucentBackground);
     menu->setObjectName("senMenu");
     sendAction = menu->addAction("Selected line");
     ctrlAction = menu->addAction("Changed direction");
 
     ui->pushButton->setCheckable(true);
     ui->pushButton_2->setMenu(menu);
     ui->pushButton_3->setMenu(menu);
     //影藏三角形,但依旧会占右侧空间,字体不居中
     ui->pushButton_3->setStyleSheet("QPushButton:menu-indicator{image:none;}");
 }

 //按钮槽函数
  void Widget::menuSlot()
  {
      QPoint pos = ui->pushButton->pos();
  
      pos.setX(pos.x() + 1);
      pos.setY(pos.y() + ui->pushButton->height() - 1);
      //计算菜单位置
      menu->exec(mapToGlobal(pos));
 }

QMenu stylesheet

QMenu *menu = new QMenu();
 
    menu->addAction("aaaa");
    menu->addAction("bbbb");
    menu->addAction("cccc");
 
    menu->show();
    menu->move(600,400);
 
    menu->setStyleSheet(
                " QMenu {\
                background-color: white; /* sets background of the menu 设置整个菜单区域的背景色,我用的是白色:white*/\
                border: 1px solid white;/*整个菜单区域的边框粗细、样式、颜色*/\
            }\
            QMenu::item {\
                /* sets background of menu item. set this to something non-transparent\
                    if you want menu color and menu item color to be different */\
                background-color: transparent;\
                padding:8px 32px;/*设置菜单项文字上下和左右的内边距,效果就是菜单中的条目左右上下有了间隔*/\
                margin:0px 8px;/*设置菜单项的外边距*/\
                border-bottom:1px solid #DBDBDB;/*为菜单项之间添加横线间隔*/\
            }\
            QMenu::item:selected { /* when user selects item using mouse or keyboard */\
                background-color: #2dabf9;/*这一句是设置菜单项鼠标经过选中的样式*/\
            }"
 
                );

QMenu的QAction::triggered信号响应

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //将UI上面的控件和槽函数建立连接
    //文件菜单中的内容
    connect(ui->Open_File,&QAction::triggered,this,&MainWindow::OpenFileAction);
    connect(ui->actionSave,&QAction::triggered,this,&MainWindow::SaveFileAction);
    connect(ui->actionsave_new, &QAction::triggered, this, &MainWindow::SaveNewFileAction);

}

//文件下拉列表中打开文件的操作函数
void MainWindow::OpenFileAction()
{
    QMessageBox::information(NULL, QString("title"), QString("现在点击打开文件"));
    return;
}

//文件下拉列表中保存文件的操作
void MainWindow::SaveFileAction()
{
    QMessageBox::information(NULL, QString("title"), QString("现在点击保存文件"));
    return;
}

//文件下拉列表中另存为文件的操作
void MainWindow::SaveNewFileAction()
{
    QMessageBox::information(NULL, QString("title"), QString("savenew"));
    return;
}

QMenuStylesheet

MyMenu->setStyleSheet(
                     "\
                     QMenu {\
                     background-color:rgb(89,87,87); /*整个背景*/\
                     border: 3px solid rgb(235,110,36);/*整个菜单边缘*/\                     
                     }\
                 QMenu::item {\                    
                     font-size: 10pt; \
                     color: rgb(225,225,225);  /*字体颜色*/\
                     border: 3px solid rgb(60,60,60);    /*item选框*/\
                     background-color:rgb(89,87,87);\
                     padding:16px 16px; /*设置菜单项文字上下和左右的内边距,效果就是菜单中的条目左右上下有了间隔*/\
                     margin:2px 2px;/*设置菜单项的外边距*/\
                      }\
                 QMenu::item:selected { \
                     background-color:rgb(235,110,36);/*选中的样式*/\
                     }\
                 QMenu::item:pressed {/*菜单项按下效果*/\
                                           border: 1px solid rgb(60,60,61); \
                                           background-color: rgb(220,80,6); \
                                       }\      
                    "

 设置菜单项高度

方法一

popupMenu->setStyleSheet("QMenu::item{ padding:100px;}");

方法二

class MyMenuItem:public QWidget
{
    Q_OBJECT
    public:
    MyMenuItem(QWidget *parent)
    {
        new QLabel("test",this);
    }
};

 MyMenuItem *item1 = new MyMenuItem(this);
    item1->setFixedSize(100,100);//这里可以设置大小
    QWidgetAction *action1 = new QWidgetAction(popupMenu);
    action1->setDefaultWidget(item1);
    MyMenuItem *item2 = new MyMenuItem(this);
    QWidgetAction *action2 = new QWidgetAction(popupMenu);
    action2->setDefaultWidget(item2);
    MyMenuItem *item3 = new MyMenuItem(this);
    QWidgetAction *action3 = new QWidgetAction(popupMenu);
    action3->setDefaultWidget(item3);

QMenu带icon的样式表设置

QMenu {
    background-color: #292F38;
    color:#CFCFCF;
    padding:0px;
    margin:0px;
    border: 1px solid gray;
}
QMenu::item {
    padding-left:20px;
    padding-right:40px;
    padding-top:5px;
    padding-bottom:5px;
}
QMenu::icon{
    position:absolute;
    left:10px;
}
QMenu::item:hover {
    border-color: #3CA9FE;
    background: #3CA9FE;
    color:#FFFFFF;
    padding-left:0px;
    padding-right:0px;
}
QMenu::item:selected {
    border-color: #3CA9FE;
    background: #3CA9FE;
    color:#FFFFFF;
}
QMenu::item:checked {
    border-color: #3CA9FE;
    background: #3CA9FE;
    color:#FFFFFF;
}
QMenu::separator {
    height: 2px;
    background: lightblue;
    margin-left: 10px;
    margin-right: 5px;
}
QMenu::indicator {
    width: 15px;
    height: 15px;
}
/* exclusive indicator = radio button style indicator (see QActionGroup::setExclusive) */
QMenu::indicator:exclusive:checked {
    border-color: #3CA9FE;
    background: #3CA9FE;
    color:#ffffff;
}
QMenu::indicator:exclusive:checked:selected {
    border-color: #3CA9FE;
    background: #3CA9FE;
    color:#ffffff;
}

 右键上下文菜单实现

#ifndef MYMENU_H
#define MYMENU_H

#include <QWidget>

class MyMenu : public QWidget
{
    Q_OBJECT
public:
    MyMenu(QWidget *parent = nullptr);
protected:
    void contextMenuEvent(QContextMenuEvent *event) override;
private:
    QAction * colorChat;
};
#endif // MYMENU_H
#include "mymenu.h"
#include <QMenu>
#include <QFile>
#include <QAction>
#include <QString>
#include <QDebug>

MyMenu::MyMenu(QWidget *parent)
    : QWidget(parent)
{
    QFile file("H:/Qt/contextMenu/ContextMenu/style.qss");
    file.open(QFile::ReadOnly);//读取qss文件,设置样式
    if(file.isOpen())
    {
        QString qss = file.readAll();
        //qDebug()<<qss;
        this->setStyleSheet(qss);
    }
    file.close();
    
    //带选择的QAction应该设置为类的成员变量,以便存住当前状态
    colorChat = new QAction(QString::fromLocal8Bit("   使用多彩气泡"),this);
    colorChat->setCheckable(true);
    colorChat->setChecked(true);
    colorChat->setIcon(QIcon(":/choose.png"));
    connect(colorChat,&QAction::triggered,this,[=]()
    {
         if(colorChat->isChecked())
             colorChat->setIcon(QIcon(":/choose.png"));
         else
             colorChat->setIcon(QIcon(":/space.png"));
    });
}

void MyMenu::contextMenuEvent(QContextMenuEvent *event)
{
    QAction * start = new QAction(QString::fromLocal8Bit("   复制"),this);
    QAction * allchoose = new QAction(QString::fromLocal8Bit("   全部选择"),this);
    QAction * choosesome = new QAction(QString::fromLocal8Bit("   多选"),this);
    choosesome->setIcon(QIcon(":/duoxuankuang.png"));
    QAction * showType = new QAction(QString::fromLocal8Bit("   显示比例"),this);
    QAction * setColorChat = new QAction(QString::fromLocal8Bit("   设置多彩气泡"),this);
    QAction * quit = new QAction(QString::fromLocal8Bit("   清屏"),this);

    QMenu * m = new QMenu(this);
    m->setFixedWidth(100);
    m->addAction(new QAction("  200%"));
    m->addAction(new QAction("  180%"));
    m->addAction(new QAction("  160%"));
    m->addAction(new QAction("  140%"));
    m->addAction(new QAction("  120%"));
    m->addAction(new QAction("  100%"));
    m->setTitle(QString::fromLocal8Bit("   显示比例"));

    QMenu * menu = new QMenu(this);
    menu->setFixedWidth(200);
    menu->addAction(start);
    menu->addAction(allchoose);
    menu->addAction(choosesome);
    menu->addMenu(m);
    menu->addSeparator();
    menu->addAction(showType);
    menu->addAction(colorChat);
    menu->addAction(setColorChat);
    menu->addSeparator();
    menu->addAction(quit);

    menu->exec(QCursor::pos());

    return QWidget::contextMenuEvent(event);
}

 

QMenu {
    background-color : rgb(253,253,254);
 padding:5px;
 border-radius:15px;
}
QMenu::item {
    font-size:10pt;
    color: rgb(0,0,0);
    background-color:rgb(253,253,254);
    padding: 8px 25px 6px 10px;
    margin: 4px 1px;
}
QMenu::item:selected {
    background-color : rgb(236,236,237);
}
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);
    background-image: url(:/space_selected.png);
}
QMenu::separator {
    height: 2px;
    background: rgb(235,235,236);
    margin-left: 10px;
    margin-right: 10px;
}

在这里插入图片描述

 

 

在这里插入图片描述

 

在这里插入图片描述

 

 

在这里插入图片描述

 

美化前效果 

在这里插入图片描述

 

美化后效果 

在这里插入图片描述

 

QMenu自定义icon大小

QMenu默认的icon最大是16px*16px,而且通过stylesheet还改不了。

问题症结:QMenu 使用 QProxyStyle 里面的QStyle::PM_SmallIconSize

解决办法: 1.定义一个新的Style继承自QProxyStyle 替换掉QStyle::PM_SmallIconSize
 

#include <QProxyStyle>

class CustomIconStyle : public QProxyStyle
{
    Q_OBJECT

public:
    CustomIconStyle();
    ~CustomIconStyle();

    void SetCustomSize(int);
protected:
    virtual int pixelMetric(PixelMetric metric, const QStyleOption * option, const QWidget * widget) const;

private:
    int mSize;
};

#endif // CUSTOMICONSTYLE_H
#include "customiconstyle.h"
CustomIconStyle::CustomIconStyle()
{
    mSize = 30;
}

CustomIconStyle::~CustomIconStyle()
{

}


void CustomIconStyle::SetCustomSize( int nSize )
{
    mSize = nSize;
}

int CustomIconStyle::pixelMetric( PixelMetric metric, const QStyleOption * option, const QWidget * widget ) const
{
    int s = QCommonStyle::pixelMetric(metric, option, widget);
    if (metric == QStyle::PM_SmallIconSize) {
        s = mSize;
    }

    return s;

}
CustomIconStyle* pStyle = new CustomIconStyle;
    m_logoMenu->setStyle(pStyle);
    m_logoMenu->setStyleSheet(

参考

  1.  qt中设置菜单高度
  2. 【QT】Qaction和触发函数建立连接的方法 
  3. qt 菜单/右键快捷菜单 选项不可操作/背景/分隔线颜色样式表
  4. Qt实现多级菜单及设置菜单的宽度
  5. Qt按钮添加菜单
  6.  QMenu自定义icon大小

 

  • 10
    点赞
  • 83
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值