Customize QMenu display with QProxyStyle

John Sturtz john at sturtz.org
Mon Jun 9 17:29:56 BST 2025


Thanks again Charles.  In searching around, I found some examples that 
reimplemented drawItemText() and some that reimplemented drawControl().  
The former seemed more intuitive to me (goes to show you what I know), 
so I went down the wrong rabbit hole.

One more question if you will:  I've attached a newer version of my 
original sample, based on your suggestion.

If I install the proxy style for the menu with:

     file_menu.setStyle(Style())

it works (items are right-justified).  But the menu has a slightly 
different appearance (on my system, rounded corners).  Not surprising, I 
suppose.  No arguments are passed to the Style() constructor, so it's 
creating one from scratch.

I'd think the proper thing to do would be to derive the new style from 
the menu's previously-existing style:

     file_menu.setStyle(Style(file_menu.style()))

Wondering if this is a/the proper thing to do?  It seems to work, in 
that the menu then has the proper appearance (that is, one that is 
consistent with its appearance if I don't assign the proxy).

But then when I exit the app, there's a long(ish -- about two seconds) 
delay.  If I run the app from a Command Prompt window, it's a couple 
seconds before the command prompt reappears -- I think the app is not 
merely exiting, but actually crashing.  Which makes me wonder if this is 
somehow not right?

Thanks again!

/John


------ Original Message ------
>From "Charles" <peacech at gmail.com>
To "John Sturtz" <john at sturtz.org>
Cc pyqt at riverbankcomputing.com
Date 6/9/2025 12:32:17 AM
Subject Re: Customize QMenu display with QProxyStyle

> >  Wrong QProxyStyle method. Tip: to know the right method, search for 
>menuitem in  
>https://codebrowser.dev/qt6/qtbase/src/widgets/styles/qcommonstyle.cpp.html
>
>Sorry, this is wrong. QMenu is drawn by the OS style plugin and does 
>not call drawItemText. For example see: 
>https://codebrowser.dev/qt6/qtbase/src/widgets/styles/qwindowsstyle.cpp.html#1012
>
>On Mon, Jun 9, 2025 at 12:22 PM Charles <peacech at gmail.com> wrote:
>>Wrong QProxyStyle method. Tip: to know the right method, search for 
>>menuitem in 
>>https://codebrowser.dev/qt6/qtbase/src/widgets/styles/qcommonstyle.cpp.html
>>
>>class Style(QProxyStyle):
>>     def drawControl(self, element, option, painter, widget):
>>         if element == QStyle.ControlElement.CE_MenuItem:
>>             super().drawItemText(painter, option.rect, 
>>Qt.AlignmentFlag.AlignRight, option.palette, True, option.text, 
>>QPalette.ColorRole.ButtonText)
>>         else:
>>             return super().drawControl(element, option, painter, 
>>widget)
>>
>>On Mon, Jun 9, 2025 at 11:14 AM John Sturtz <john at sturtz.org> wrote:
>>>Hello again PyQt sages.  Hoping for some insight here -- despite a 
>>>few hours' time fiddling with this, I don't seem to even be getting 
>>>past square one.
>>>
>>>I'm trying to modify display of items in a QMenu using QProxyStyle.  
>>>Basically, I've defined a class named Style that derives from 
>>>QProxyStyle, and re-implements drawItemText() (which, just for 
>>>starters, tries to right-justify the menu item text).
>>>
>>>I create a QMenu object, create an object of the Style class, and 
>>>call .setStyle() to set it as the menu's style.
>>>
>>>It may or may not be the case that my drawItemText() implementation 
>>>successfully right-justifies the text.  I'll never know, because it 
>>>never gets called.  What (probably really basic thing) am I missing?
>>>
>>>Thanks!  [short sample code attached]
>>>
>>>/John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20250609/aaca87cd/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.py
Type: text/x-python
Size: 1434 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20250609/aaca87cd/attachment.py>


More information about the PyQt mailing list