标签:qt qmenu 样式表
闲来无事,发现酷我音乐盒的托盘弹出菜单蛮有个性的,于是就按照原样写了个,素材什么的都从网上下的,供大家学习,主要是它的第一项为一个Widget,而Qt的菜单项为QAction,而且这个QAction还不继承于QWidget,后来发现了有个QWidgetAction,这里可以添加自己的Widget,关键要实现一个createWidget()的虚函数。
//菜单初始化 void QMenuTest::contentMenuInit() { m_pContentMenu = new QMenu(this); Q_ASSERT(m_pContentMenu); SubControlAction* pControlAction = new SubControlAction(m_pContentMenu); m_pContentMenu->addAction(pControlAction); m_pContentMenu->addSeparator(); QMenu* pModelMenu = new QMenu(m_pContentMenu); pModelMenu->addAction(tr("单曲播放")); pModelMenu->addAction(tr("单曲循环")); pModelMenu->addAction(tr("顺序播放")); pModelMenu->addAction(tr("循环播放")); pModelMenu->addAction(tr("随机播放")); pModelMenu->setIcon(QIcon(":/res/Resources/model.png")); pModelMenu->setTitle(tr("播放模式")); m_pContentMenu->addMenu(pModelMenu); m_pContentMenu->addAction(tr("显示桌面歌词")); m_pContentMenu->addAction(tr("显示音乐树")); m_pContentMenu->addAction(QIcon(tr(":/res/Resources/setting.png")),tr("设置")); m_pContentMenu->addAction(QIcon(tr(":/res/Resources/exit.png")),tr("退出")); } //SubControlAction继承QWidgetAction,下面是.h文件 #ifndef SUBCONTROLACTION_H #define SUBCONTROLACTION_H #include <QWidgetAction> #include "MenuSubControl.h" class SubControlAction : public QWidgetAction { Q_OBJECT public: SubControlAction(QObject *parent); ~SubControlAction(); protected: QWidget* createWidget(QWidget* parent); private: MenuSubControl* m_pSubControl; }; #endif // SUBCONTROLACTION_H
//SubControlAction .cpp #include "StdAfx.h" #include "SubControlAction.h" SubControlAction::SubControlAction(QObject *parent) : QWidgetAction(parent) { } SubControlAction::~SubControlAction() { } QWidget* SubControlAction::createWidget(QWidget* parent) { m_pSubControl = new MenuSubControl(parent); Q_ASSERT(m_pSubControl); return m_pSubControl; } #ifndef MENUSUBCONTROL_H #define MENUSUBCONTROL_H #include <QWidget> #include "ui_MenuSubControl.h" class MenuSubControl : public QWidget { Q_OBJECT public: MenuSubControl(QWidget *parent = 0); ~MenuSubControl(); private: Ui::MenuSubControl ui; }; #endif // MENUSUBCONTROL_H
#include "StdAfx.h"
#include "MenuSubControl.h"
MenuSubControl::MenuSubControl(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
QFile f(":/QSS/SubControl_qss");
f.open(QIODevice::ReadOnly);
setStyleSheet(f.readAll());
}
MenuSubControl::~MenuSubControl()
{
}
ui
样式表
QMenu {
background-color: white;
border: 1px solid gray;
border-style: outset;
border-radius:2px;
padding:2px;
color:black;
}
QMenu::item {
padding: 2px 25px 2px 20px;
border: 1px solid transparent; /* reserve space for selection border */
}
QMenu::item:selected {
background: rgba(85, 85, 255, 150);
}
QMenu::icon:checked { /* appearance of a ‘checked‘ icon */
background: gray;
border: 1px inset gray;
position: absolute;
top: 1px;
right: 1px;
bottom: 1px;
left: 1px;
}
QMenu::separator {
height: 1px;
background: gray;
margin-left: 10px;
margin-right: 5px;
}
QMenu::indicator {
width: 13px;
height: 13px;
}
QPushButton#btn_Switch{
background-color:white;
border-image:url(:/res/Resources/play.png);
min-height:32px;
min-width:32px;
}
QPushButton#btn_Switch:pressed{
border-image:url(:/res/Resources/pause.png);
}
QPushButton#btn_Up{
background-color:white;
border-image:url(:/res/Resources/step_forward.png);
height:24px;
width:24px;
}
QPushButton#btn_Next{
background-color:white;
border-image:url(:/res/Resources/step_next.png);
height:24px;
width:24px;
}
标签:qt qmenu 样式表
原文地址:http://hardesi.blog.51cto.com/8493241/1588839