QMenu:如何自定义QMenu的菜单项
QMenu: How to customize the menu items of QMenu
我想用QPushButton和QMenu建立一个下拉列表控件,如下所示:
QPushButton* menuBt = new QPushButton("Please select");
menuBt->setFlat(true);
QMenu* menu = new QMenu();
menuBt->setMenu(menu);
QWidgetAction* wa1 = new QWidgetAction(menu);
QLabel* l1 = new QLabel("Option1");
wa1->setDefaultWidget(l1);
menu->addAction(wa1);
QWidgetAction* wa2 = new QWidgetAction(menu);
QLabel* l2 = new QLabel("Option2");
wa2->setDefaultWidget(l2);
menu->addAction(wa2);
menu->setStyleSheet("QMenu::item {font-family: "Arial"; font-size: 13pt; color: #808080; border: 1px solid gray; background-color: rgb(234,234,234);}"
"QMenu::item:hover {background-color: rgb(0, 0, 255);}");
menuBt->setStyleSheet("QPushButton {font-family: "Arial"; font-size: 13pt; color: #808080; border: 1px solid gray;padding: 1px 18px 1px 3px;min-width: 6em; background-color: rgb(234,234,234);}");
我通过setStyleSheet设置了菜单项的字体和悬停背景色,但似乎不起作用。如何使字体和悬停背景颜色在菜单项上工作?
答:
class QTDropDownButton : public QPushButton
{
Q_OBJECT
public:
QTDropDownButton(QString text, QWidget *parent = nullptr);
void addItem(QString text);
protected slots:
void menuAboutToShow();
private:
QMenu* menu_;
};
QTDropDownButton::QTDropDownButton(QString text, QWidget *parent) :
QPushButton(text, parent)
{
setFlat(true);
menu_ = new QMenu();
setMenu(menu_);
connect(menu_, SIGNAL(aboutToShow()), this, SLOT(menuAboutToShow()));
setStyleSheet("font-family: Arial; font-size: 13pt; color: #808080; border: 1px solid gray; background-color: rgb(234,234,234);");
menu_->setStyleSheet("QMenu::item {font-family: Arial; font-size: 13pt; color: #808080; border: 1px solid gray; background-color: rgb(234,234,234);}"
"QMenu::item:selected {background-color: rgb(0, 255, 255);}"
"QLabel {font-family: Arial; font-size: 13pt;}"
"QLabel:hover {background-color: rgb(0, 0, 255);}");
}
void QTDropDownButton::addItem(QString text)
{
if(!menu_)
return;
QWidgetAction* wa1 = new QWidgetAction(menu_);
QLabel* l1 = new QLabel(text);
wa1->setDefaultWidget(l1);
menu_->addAction(wa1);
}
void QTDropDownButton::menuAboutToShow()
{
if(menu_)
menu_->setFixedWidth(this->width());
}
要设置字体族,您不需要在Arial周围加上引号。我相信这会阻止你的样式表正确解析。
一个边注:目前只有你的菜单是样式的,其他按钮将看起来像默认按钮。要更改菜单中所有按钮的样式,将样式移动到菜单的setStylesheet()
调用中,如下所示:
menu->setStyleSheet("QMenu::item {font-family: Arial; font-size: 13pt; color: #808080; border: 1px solid gray; background-color: rgb(234,234,234);}" +
"QMenu::item:hover {background-color: rgb(0, 0, 255);}" +
"QPushButton {font-family: Arial; font-size: 13pt; color: #808080; border: 1px solid gray;padding: 1px 18px 1px 3px;min-width: 6em; background-color: rgb(234,234,234);}");
但是如果你只想让这一个按钮看起来不一样,调用是正确的setStylesheet()
,但您可以省略选择器,如下所示:
menuBt->setStyleSheet("font-family: Arial; font-size: 13pt; color: #808080; border: 1px solid gray;padding: 1px 18px 1px 3px;min-width: 6em; background-color: rgb(234,234,234);");
相关文章:
- Adobe Acrobat插件-在上下文菜单中创建子菜单项
- 如果未选中任何项,则禁用某些上下文菜单项
- 如何在Win32 API中知道选定的菜单项
- 自定义Skype菜单项
- 获得菜单项
- 使用3ds max sdk禁用/启用四元菜单项
- 删除菜单项后,如何在视觉工作室中删除资源符号,当它说"in use"?
- C++ (无 MFC ) 禁用/变灰菜单项
- 如何在c++中创建Radio菜单项
- 如何动态更新护士中的菜单项
- 如何使用Qt在OS文件管理器中添加上下文菜单项
- 拥有 HMENU,如何枚举菜单项并以编程方式调用菜单项
- 如何在MFC功能包中动态添加带有图像的菜单项
- 向菜单项添加子菜单项
- 当我在win32项目c++应用程序中单击菜单项时,如何编写代码来加载一个特殊的对话框?
- 没有修改键的菜单项
- 将菜单项位置引用到同一菜单中的另一个菜单项
- QMenu:如何自定义QMenu的菜单项
- 修改菜单项
- 在C++中根据鼠标位置查找菜单项