C++ 如何设置Qt中所选菜单::项::的样式?
我想在用户设置焦点时设置菜单项边框的颜色。第一个-item::selected,这是正确的属性吗?如果是,如何设置?是否使用设计器或在C++代码中手动操作?< /P>C++ 如何设置Qt中所选菜单::项::的样式?,c++,qt,qtstylesheets,C++,Qt,Qtstylesheets,我想在用户设置焦点时设置菜单项边框的颜色。第一个-item::selected,这是正确的属性吗?如果是,如何设置?是否使用设计器或在C++代码中手动操作?< /P> Customizing QMenu Individual items of a QMenu are styled using the 'item' subcontrol as follows: QMenu { background-color: #ABABAB; /* sets b
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; }
好的,但是在Qt5.0中把这个放在哪里呢?我是否在样式表属性的设计器中使用此选项?我想没有。在我的.cpp文件中?我可以在菜单上执行setStyleSheet
,但如何指定::item::selected
#include "MainWindow.h"
#include <QtWidgets/QWidget>
#include <QtWidgets/QMessageBox>
cf16tradingclient_1::cf16tradingclient_1(){
widget.setupUi(this);
widget.menuMarket->setStyleSheet(?????????) // I want item::selected
}
#包括“MainWindow.h”
#包括
#包括
cf16tradingclient_1::cf16tradingclient_1(){
setupUi(这个);
widget.menuMarket->setStyleSheet(???)//我想要项::选中
}
您只需执行以下操作:
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可能有更多的样式选项