c++ - QMenu:如何自定义QMenu的菜单项

标签 c++ qt qpushbutton 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 两边加上引号。我相信这会阻止您的样式表正确解析。

附注:目前只有 menuBt 具有样式,其他按钮看起来像默认按钮。要更改菜单中所有按钮的按钮样式,请将样式移动到菜单的 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);");

关于c++ - QMenu:如何自定义QMenu的菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31399610/

相关文章:

c++ - 模板类作为模板类参数

c++ - 在 QListView 中显示 vector

c++ - QML:QQmlListProperty<T>。 T 必须是 QObject 吗?

c++ - 如何在 QT Creator 中自动包含所需的标题

c++ - Qt 5 将带有参数的插槽分配给 QPushButton

Qt 按钮信号和槽

c++ - 选择排序数组

c++ - 增加 X11 R6 中的字体大小和属性

c++ - std::hash_set 中定义的 stdext::hash_value() 的性能

qt - 在qt中的QTableView列中添加两种不同颜色的文本