Customize QMenu display with QProxyStyle

Charles peacech at gmail.com
Mon Jun 9 06:32:17 BST 2025


>  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/f0fedb67/attachment.htm>


More information about the PyQt mailing list