QMenu:如何自定义QMenu的菜单项

QMenu: How to customize the menu items of QMenu

本文关键字:QMenu 菜单项 自定义      更新时间:2023-10-16

我想用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);");