1

当用户设置焦点时,我想设置菜单项边框的颜色。第一个 - item::selected,这是正确的属性吗?如果是,如何设置?我是使用 Designer 还是在 C++ 代码中手动完成?

Customizing QMenu Individual items of a QMenu are styled using the 'item' subcontrol as follows:  

         QMenu {
             background-color: #ABABAB; /* sets background of the menu */
             border: 1px solid black;  }

         QMenu::item {
             /* sets background of menu item. set this to something non-transparent
                 if you want menu color and menu item color to be different */
             background-color: transparent;  }

         QMenu::item:selected { /* when user selects item using mouse or keyboard */
             background-color: #654321;  }

好的,但是把它放在 Qt 5.0 的什么地方呢?我是否在样式表属性的Designer中使用它?我觉得不是。在我的 .cpp 文件中?我可以在我的菜单上做,但如何指定 ::item::selected?setStyleSheet

#include "MainWindow.h"
#include <QtWidgets/QWidget>
#include <QtWidgets/QMessageBox>
cf16tradingclient_1::cf16tradingclient_1(){
    widget.setupUi(this);
    widget.menuMarket->setStyleSheet(?????????) // I want item::selected
}
4

1 回答 1

2

你只需这样做:

widget.menuMarket->setStyleSheet("QMenu::item:selected{border:1px solid red;}");

如果您只希望一个特定的菜单具有这样的行为,那么这是正确的地方。(我不明白为什么在 Designer 中设置它不起作用。试一试。但我自己并不熟悉。)

如果您希望所有菜单都具有相同的样式,请在QApplication级别、您的main或其他在启动时运行一次的代码中执行此操作。

QApplication app(argc, argv);
// ...
app.setStyleSheet("QMenu::item:selected {border: 5px solid yellow;}");
// ...

你可以将两者结合起来。您放置在特定小部件上的样式表(第一个示例)将覆盖全局样式表(就像 CSS 所做的那样)。

这在 Qt4 和 Qt5 中是一样的,尽管 Qt5 可能有更多的样式选项。

于 2013-05-25T06:45:29.540 回答