C++ QAction图标在悬停时更改

C++ QAction图标在悬停时更改,c++,qt,qaction,C++,Qt,Qaction,在我的项目中,我显示一个带有多个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:

在我的项目中,我显示一个带有多个
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设计器的形式快速尝试复制了你的用例(基本上是用C++代码:<代码> QICON::AddixPixMax()/Cuth>):

?xml version=“1.0”encoding=“UTF-8”>
主窗口
0
0
400
300
主窗口
0
0
400
22
假的
哟
顶部工具栏区域
假的
真的
../red-circle.png
../greeb-circle.png
../red-square.png
../green square.png../red-circle.png
福
酒吧

当使用默认的Mac样式时,我只会在菜单和工具栏中看到红色/绿色圆圈图标,即使在鼠标悬停时也是如此。但是,如果我强制使用另一种样式,例如
ui->menuYo->setStyle(QStyleFactory::create(“fusion”)然后悬停可以工作,但菜单不再是本地的…

我没有使用Mac,我的用户也不会。只有窗户。我应该期望它能像我那样在Windows上工作吗?此外,我的图像来自一个大图像,因此使用Qt Designer对我的用例不起作用。我手头没有Windows机器,所以我无法确定它是否能与本机样式一起工作。但至少根据你的方法,它应该是有效的。请注意,
QIcon::Selected
不适用于菜单。您需要的是
QIcon::Normal
(==未覆盖)和
QIcon::Active
(==悬停),而
QIcon::Off
QIcon::On
仅适用于可检查的操作(即具有已检查/未检查状态的操作)。测试是您的代码还是样式的一种方法是使用使用fusion小部件样式的参数
-style fusion
运行应用程序。如果代码是正确的,那么这一个就可以保证工作!问题是我需要将这两种状态都设置为
QIcon::Off
。我没有意识到这与是否接受检查有关。谢谢:)
?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>