关注微信公众号在菜单栏获取源码
这里分享下,Qt下面的自定义的menu。先上效果图。
我这个只是简单的做个demo,如果你想要更复杂的效果,就要重写QPushButton或者是其他的控件,原理都是一样的。
我贴出里面的重要代码:
void CustomMenu::initMenu()
{
m_AppMenu = new QMenu(this);
m_AppMenu->setStyleSheet(
"QMenu {"
"background-color:#FFFFFF;"
"border: 2px solid #FFFFFF;"
"width:120px;"
"height:137px;"
"}"
"QMenu::separator {"
"height: 1px; "
"background: #D9DADB;"
"}"
);
// 开始游戏
QPushButton *btn = new QPushButton(tr("start game"), m_AppMenu);
btn->setMaximumWidth(116);
btn->setStyleSheet(button.arg("url(:/CustomMenu/res/menu_startup.png)"));
QWidgetAction *widgetaction = new QWidgetAction(m_AppMenu);
widgetaction->setDefaultWidget(btn);
m_AppMenu->addAction(widgetaction);
// 置顶
btn = new QPushButton(tr("set top"), m_AppMenu);
btn->setMaximumWidth(116);
btn->setStyleSheet(button.arg("url(:/CustomMenu/res/menu_top.png)"));
widgetaction = new QWidgetAction(m_AppMenu);
widgetaction->setDefaultWidget(btn);
m_AppMenu->addAction(widgetaction);
// 创建快捷方式
btn = new QPushButton(tr("create shortcut"), m_AppMenu);
btn->setMaximumWidth(116);
btn->setStyleSheet(button.arg("url(:/CustomMenu/res/menu_shortcut.png)"));
widgetaction = new QWidgetAction(m_AppMenu);
widgetaction->setDefaultWidget(btn);
m_AppMenu->addAction(widgetaction);
//加2px间距
QLabel *firstLine = new QLabel();
firstLine->setFixedSize(QSize(118, 2));
widgetaction = new QWidgetAction(m_AppMenu);
widgetaction->setDefaultWidget(firstLine);
m_AppMenu->addAction(widgetaction);
m_AppMenu->addSeparator();
firstLine = new QLabel();
firstLine->setFixedSize(QSize(118, 2));
widgetaction = new QWidgetAction(m_AppMenu);
widgetaction->setDefaultWidget(firstLine);
m_AppMenu->addAction(widgetaction);
// 删除游戏
btn = new QPushButton(tr("delete game"), m_AppMenu);
btn->setMaximumWidth(116);
btn->setStyleSheet(button.arg(""));
widgetaction = new QWidgetAction(m_AppMenu);
widgetaction->setDefaultWidget(btn);
m_AppMenu->addAction(widgetaction);
}
微信公众号回复源码,即可获取完整的项目源码。