在我的项目中,我显示一个带有几个QAction
对象的QMenu
。我希望用户徘徊时QAction
图标更改。
这是我当前的代码:
QPixmap icons(":/icons/platformIcons.png");
QIcon icon;
icon.addPixmap(icons.copy(0, 0, 16, 16), QIcon::Selected, QIcon::On);
icon.addPixmap(icons.copy(0, 16, 16, 16), QIcon::Selected, QIcon::Off);
ui->actionOpen->setIcon(icon);
但是,当用户徘徊在QAction
上时,图标不会更改。我尝试了模式Normal
和Active
,结果是相同的。如果我切换状态,则图标会逆转,但仍不会在悬停时改变(或单击此事)。
感谢您的时间。
支持悬停在菜单和工具栏中的正常/活动图标的支持似乎取决于平台样式,即使在禁用本机菜单栏的使用情况下,也不受到本机Mac样式的支持(即让菜单显示在桌面的顶部,而不是在应用程序窗口中)。
我已经快速尝试了Mac上的QT设计器表单以复制您的用例(基本上是使用QIcon::addPixmap()
最终作为相同的C 代码):
?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget"/>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>22</height>
</rect>
</property>
<property name="nativeMenuBar">
<bool>false</bool>
</property>
<widget class="QMenu" name="menuYo">
<property name="title">
<string>Yo</string>
</property>
<addaction name="actionFoo"/>
<addaction name="actionBar"/>
</widget>
<addaction name="menuYo"/>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
<addaction name="actionFoo"/>
<addaction name="actionBar"/>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="actionFoo">
<property name="checkable">
<bool>true</bool>
</property>
<property name="icon">
<iconset>
<normaloff>../red-circle.png</normaloff>
<normalon>../greeb-circle.png</normalon>
<activeoff>../red-square.png</activeoff>
<activeon>../green-square.png</activeon>../red-circle.png</iconset>
</property>
<property name="text">
<string>Foo</string>
</property>
</action>
<action name="actionBar">
<property name="text">
<string>Bar</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
使用默认的Mac样式时,即使悬停在鼠标时,我也只会在菜单和工具栏中获得红色/绿色圆圈图标。但是,如果我强迫另一种风格,例如 ui->menuYo->setStyle(QStyleFactory::create("fusion"));
然后悬停起作用,但菜单不再看起来本地...