如何自定义QMenu的菜单项

如何自定义QMenu的菜单项

本文介绍了QMenu:如何自定义QMenu的菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用QPushButton和QMenu构建一个下拉列表控件,如下所示:

I want to build a dropdown list control with QPushButton and QMenu like below:

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将字体和鼠标悬停在菜单项上,但是似乎不起作用.如何使字体和悬停背景色在菜单项上起作用?

I set the font and hover background color to the menu items by setStyleSheet, but seems it doesn't work.How make the font and hover background color work on menu items?

答案:

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上加上引号.我相信这会阻止您的样式表正确解析.

To set the font-family you don't need to put quotes around the Arial. I believe this prevents your style sheet from parsing correctly.

一个旁注:目前仅对menuBt进行样式设置,其他按钮将看起来像默认按钮.要更改菜单中所有按钮的按钮样式,请将样式移动到菜单的setStylesheet()调用中,如下所示:

A side note: at the moment only your menuBt is styled, other buttons will look like default buttons. To change button style for all buttons in the menu, move the style into the setStylesheet() call of the menu like this:

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(),但是您可以省略选择器,如下所示:

But if you want only this one button to look different, it is correct to callsetStylesheet() on it, but you can omit the selector, like this:

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);");

这篇关于QMenu:如何自定义QMenu的菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:23