<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small">Looking at the source, I guess the highlight does not persist because setCurrentAction calls QMenuPrivate::setCurrentAction using SelectedFromElsewhere as the reason parameter. On the other hand if I select the menu item using the keyboard, the highlight persists.</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small">So another option is to select the menu item by simulating keypress:</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"><span style="font-family:monospace">def _highlight_action(self, target):<br>  actions = self.actions()<br>  active = self.activeAction()<br>  n = actions.index(target) - actions.index(active) if active else actions.index(target) + 1<br>  key = Qt.Key.Key_Up if n < 0 else Qt.Key.Key_Down<br>  for _ in range(abs(n)):<br>    self.keyPressEvent(QKeyEvent(QEvent.Type.KeyPress, key, Qt.KeyboardModifier.NoModifier))<br>  if target.menu():<br>    self.keyPressEvent(QKeyEvent(QEvent.Type.KeyPress, Qt.Key.Key_Enter, Qt.KeyboardModifier.NoModifier))</span><br><br></div></div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Thu, Mar 6, 2025 at 9:36 AM John Sturtz <<a href="mailto:john@sturtz.org">john@sturtz.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="msg-87243191271088943"><div><div><span>------ Original Message ------</span></div><div>
<div>From "Charles" <<a href="mailto:peacech@gmail.com" target="_blank">peacech@gmail.com</a>></div>
<div>To "John Sturtz" <<a href="mailto:john@sturtz.org" target="_blank">john@sturtz.org</a>></div>
<div>Cc <a href="mailto:pyqt@riverbankcomputing.com" target="_blank">pyqt@riverbankcomputing.com</a></div>
<div>Date 3/5/2025 7:15:55 PM</div>
<div>Subject Re: Peculiar behavior trying to set active menu item</div></div><div><br></div>
<div id="m_-87243191271088943x6e209a969b00487"><blockquote cite="http://CABthHP-xz_QQvAwQqb5nQKK7gUPb5VpxtaUbi41T7qioFJTiAg@mail.gmail.com" type="cite" class="m_-87243191271088943cite2">
<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small">If you look at <a href="https://codebrowser.dev/qt6/qtbase/src/widgets/widgets/qmenu.cpp.html#_ZN5QMenu10timerEventEP11QTimerEvent" target="_blank">https://codebrowser.dev/qt6/qtbase/src/widgets/widgets/qmenu.cpp.html#_ZN5QMenu10timerEventEP11QTimerEvent</a>, QMenu has a timerEvent. This resets the highlight.</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small">What you can do is store the active item in an attribute and reselect it after the timerEvent</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"><br></div><span style="font-family:monospace"><span class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"></span></span><span style="font-family:monospace">def timerEvent(self, event):<br>    super().timerEvent(event)<br>    if self._active_action:<br>      self.setActiveAction(self._active_action)</span><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"><br></div></div></blockquote><div id="m_-87243191271088943xd3c937194b6d40468a28374c2663da79"><div>Without having looked at the source, I was already coming to that conclusion. The time duration before the highlight disappeared was so consistent, it felt like it must be controlled by a timer.</div><div><br></div><div>So I installed an event filter on the menu, and sure enough, shortly after typing 'A' to select the first menu item, the menu receives a <font face="Consolas" size="3" style="font-size:16px">Timer</font> event, followed by an <font face="Consolas" size="3" style="font-size:16px">UpdateRequest</font> event.</div><div><br></div><div>It isn't obvious to me why it does this. Indeed, if I just suppress it:</div><div><br></div><div><font face="Consolas">  def timerEvent(self, event):</font></div><div><font face="Consolas">    return</font></div><div><br></div><div>it seems to work fine. But I expect someone must have thought there was a good reason for it at some point, and I imagine if I took this approach, it would probably eventually bite me somehow.</div><div><br></div><div>Another possible approach is to query the active action from <font face="Consolas" size="3" style="font-size:16px">timerEvent()</font> and save it, prior to calling <font face="Consolas" size="3" style="font-size:16px">super()</font> (which I presumes is responsible for the reset), then use that to reselect it afterward:</div><div><br></div><div id="m_-87243191271088943xf7016701743b41ac8e1b49e767b061cd"><div style="background-color:rgba(0,0,0,0);margin:0px"><font face="Consolas">  def timerEvent(self, event):</font></div><div style="background-color:rgba(0,0,0,0);margin:0px"></div></div><div><font face="Consolas" size="3" style="font-size:16px">    active_action = self.activeAction()</font></div><div><font face="Consolas" size="3" style="font-size:16px">    super().timerEvent(timer)</font></div><div><font face="Consolas" size="3" style="font-size:16px">    self.setActiveAction(active_action)</font></div><div style="font-size:16px"><br></div><div>This seems to work also.</div><div><br></div><div>Thank you again for your help!</div><div><br></div><div>/John</div></div></div>
</div></div></blockquote></div>