Although your question is not strictly pyqt-related (therefore a bit
off-topic), I feel the urge to add further considerations, also considering
what Charles has already written.
First of all, a QProxyStyle always has a "base style", and takes full
ownership of it. That style is used as a base for everything that the proxy
doesn't implement on its own.
- when using the QProxyStyle constructor without arguments, it always
creates a new instance of the native style (the one normally used when a
new QApplication is created) as its base: doing app.setStyle(Style())
will **not** use the style eventually used with a previous
app.setStyle(<some
other style>) as its base;
- a new style instance is also created when using the string
constructor: QProxyStyle('fusion') is syntactic sugar for
QProxyStyle(QStyleFactory.create('fusion')) (as explained in the related
docs);
- when explicitly using an *existing* QStyle instance for its
constructor argument, it will use that specific style instance as its base;
In all cases, though, the proxy *always* takes full ownership of the base
style, no matter what.
This means that you should be **very** careful in trying to use the style
of a widget as a base for the proxy: doing something like
"widget.setStyle(MyStyle(widget.style())" can have catastrophic results.
Remember that, by default, all widgets use the application style object
(meaning it's the same instance): QWidget.style() does not return a
"unique" style instance for the widget, but the style the widget is using:
in normal conditions, widget.style() is the **same object** as
QApplication.style().
Creating a proxy style with a QStyle instance as its base, that is also (or
potentially) used elsewhere, is quite dangerous: the most important issue
is when the proxy is eventually deleted, which is something that happens
when the widget it's used on is deleted: even though you may not explicitly
delete the widget, the deletion may happen whenever any parent/owner of
that widget is, something that also happens when the QApplication is being
quit, as it needs to properly "clean up" all QObjects before actually
quitting and returning its exit code.
This is exactly the reason for the delay and (possibly silent) crash you
see when closing the program; while the deletion order of sibling widgets
may be completely arbitrary, it always follows the object tree (a parent is
deleted only as soon as all its children are): if you set a proxy style for
a widget using a style that was not owned by that widget, when the widget
is deleted it will also delete the proxy *and* the base style, but that
style is potentially also (and still) being used by other widgets (and the
application) as well, leading to a segmentation fault due to attempting to
access a no-more existent object during the deletion process.
It may be possible to reparent the base style after setting the proxy (for
instance, reparenting to the QApplication instance), but that would
probably be inappropriate anyway: not only I'm not completely sure it would
still make it safe enough, but QProxyStyle also calls an internal
setProxy() function, meaning that some QStyle functions may still rely on
the newly set proxy even for widgets that still use the original style.
If you only want to target *one* specific widget instance (or subclass
instance), the safest approach is to use the string or QStyleFactory way,
using the QApplication style. The following should suffice:
baseStyleName = QApplication.style().objectName()
myWidget.setStyle(MyStyle(baseStyleName))
# which is identical to:
myWidget.setStyle(MyStyle(QStyleFactory.create(baseStyleName)))
Note: the above relies on the style object name, it only works for standard
and properly implemented QStyles (those that have object names that match
the results of QStyleFactory.keys()) and when *not* using stylesheets (read
more below on this).
If you instead want to target all widgets of that same type (in this case,
all menus), you can just create the proxy instance without arguments, and
set it for the whole application.
That said, as Charles wrote, overriding drawItemText alone is
inappropriate, as it's almost always insufficient.
The only occurrence I'm aware of a widget directly calling drawItemText()
is from for QLabels that only have plain text set (or that force the
PlainText textFormat, instead of the default AutoText).
Any other widget type will use QStyle functions such as drawPrimitive(),
drawControl() or drawComplexControl(), and it's up to the style to
*eventually* call drawItemText internally: that function is just provided
as a convenience that *could* be used by a style, but styles are not
required to use it.
Some styles do call it in some cases, but not in others (usually relying on
the basic QPainter.drawText()) and there is absolutely no consistency
required for that. Some styles even have their own internal functions to
draw text depending on the widget or [sub]control type, as a more advanced
alternative to drawItemText.
QStyleSheetStyle (the private style used whenever a style sheet affects a
widget) *does* use drawItemText for many widgets, and, in fact, that's one
of the few functions that can be effectively overridden in a proxy style
when using style sheets, but it's largely pointless as it doesn't provide
any context of the widget that is being drawn, and also requires the
stylesheet to actually affect the display of the widget: if the QSS rules
don't affect the widget, then QStyleSheetStyle will just use the style it's
currently based on: if its own base style is a proxy, and that proxy
doesn't use drawItemText, then we're back to square one.
Interestingly enough, QStyleSheetStyle does not use drawItemText for QMenu
items, therefore it's completely useless for this case.
Regarding the note about the code snippet above, since setting a stylesheet
on the application (or on widgets) internally sets a QStyleSheetStyle as
"primary" style, QApplication.style() or QWidget.style() will return a
style that has an empty object name. Trying to go through the meta object
system would be ineffective as well, as style().metaObject().className()
will obviously return "QStyleSheetStyle". QStyleSheetStyle *does* have a
baseStyle() function (similar to that of QProxyStyle), but it's
unfortunately private (I've submitted
https://bugreports.qt.io/browse/QTBUG-132201 about this, but it's been
labeled for Qt7). The only way to work around this, in case you need to
apply application-wide style sheets, is to get the default style name as
soon as the QApplication is created (but *before* setting any QSS) and keep
a reachable reference to it, either as a global variable, or as a dynamic
property of the QApplication (eg: app.setProperty('defaultStyleName',
app.style().objectName()), eventually retrievable through
QApplication.instance().property('defaultStyleName')).
The reason for which you may see rounded corners when applying the proxy
(or without setting the "fusion" style in any way) is that the default Qt
style in your system does use rounded corners.
There are only a few widgets that provide rounded corners (achieved through
QWidget.setMask()) for top level widgets: QToolTip, the popup of QComboBox,
and QMenu. This only happens if the style requires it, though: for the
above classes, the widget queries QStyle.styleHint(), and eventually calls
setMask() on itself with the returned value.
If you get rounded corners by default (without setting any style), it means
that the default style for your system uses them, therefore doing
setStyle(Style()) (without arguments) will still get you those rounded
corners, while setStyle(Style('fusion')) will not, because it will then
follow the behavior of "fusion" as its base style (which has straight
corners), just like doing setStyle('fusion') would.
Considering all the above, overriding drawControl() and checking CE_MenuItem
is normally appropriate, but there are many aspects you should consider.
First of all, you must remember that not all widgets are completely managed
by Qt. This is the case of native dialogs, or the menu bar for macos and
some linux distros. I haven't used macos for years and have never used such
unified menubars on Linux, so I'm not completely sure that overriding
QStyle painting for menu items would work in these situations.
No matter what, your attempts are quite flawed.
Even though the code obviously is a "proof of concept", your drawControl()
override is based on wrong and too simplistic assumptions; there are lots
of issues that could or should be consider, but, at the very least, you
failed to consider:
- the margins that the default style needs to properly paint an item
*within* the menu; the vertical ones may be not that relevant, but the
horizontal one are important, otherwise the text may be shown too close to
the horizontal margins;
- actions may be disabled and therefore shown differently;
- actions may contain an icon and/or a check/radio indicator (the layout
system of QMenu should make that unimportant, but still relevant to
consider);
- proper color roles (disabled/highlighted actions normally have
different background and text colors);
- border/background drawing (styles usually draw items differently
depending on their enabled/disabled and/or normal/selected states);
- hints for actions related to submenus (some styles normally display an
arrow or something similar);
- the font of the item (actions do have a font property!)
- the action shortcuts (both using the "&" prefix for "accelerators", or
key sequences);
At the very least, the override should try to use the default
implementation without the action text, and eventually attempt to draw the
text considering all possible options and some educated guesses.
An example of this attempt is shown in the answer to this post:
https://stackoverflow.com/q/59218378
It basically calls the base implementation after clearing the option text,
and then proceeds to draw the text on its own. It still makes some
assumptions (eg: the margin, no shortcuts/accelerator, no text font/color),
but it's certainly more appropriate.
Remember: as with other complex widgets, overriding the behavior of QMenu
is not an easy task. Changing the text alignment of menu items may seem a
relatively easy task, but it actually implies lots of low-level aspects,
most of which cannot be assumed.
Best regards,
MaurizioB
Il giorno lun 9 giu 2025 alle ore 06:14 John Sturtz <john at sturtz.org> ha
scritto:
> 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
>
--
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/pyqt/attachments/20250610/cc46dd6f/attachment-0001.htm>
More information about the PyQt
mailing list
‘She has never mentioned her father to me. Was he—well, the sort of man whom the County Club would not have blackballed?’ "We walked by the side of our teams or behind the wagons, we slept on the ground at night, we did our own cooking, we washed our knives by sticking them into the ground rapidly a few times, and we washed our plates with sand and wisps of grass. When we stopped, we arranged our wagons in a circle, and thus formed a 'corral,' or yard, where we drove our oxen to yoke them up. And the corral was often very useful as a fort, or camp, for defending ourselves against the Indians. Do you see that little hollow down there?" he asked, pointing to a depression in the ground a short distance to the right of the train. "Well, in that hollow our wagon-train was kept three days and nights by the Indians. Three days and nights they stayed around, and made several attacks. Two of our men were killed and three were wounded by their arrows, and others had narrow escapes. One arrow hit me on the throat, but I was saved by the knot of my neckerchief, and the point only tore the skin a little. Since that time I have always had a fondness for large neckties. I don't know how many of the Indians we killed, as they carried off their dead and wounded, to save them from being scalped. Next to getting the scalps of their enemies, the most important thing with the Indians is to save their own. We had several fights during our journey, but that one was the worst. Once a little party of us were surrounded in a small 'wallow,' and had a tough time to defend ourselves successfully. Luckily for us, the Indians had no fire-arms then, and their bows and arrows were no match for our rifles. Nowadays they are well armed, but there are[Pg 41] not so many of them, and they are not inclined to trouble the railway trains. They used to do a great deal of mischief in the old times, and many a poor fellow has been killed by them." As dusk came on nearly the whole population of Maastricht, with all their temporary guests, formed an endless procession and went to invoke God's mercy by the Virgin Mary's intercession. They went to Our Lady's Church, in which stands the miraculous statue of Sancta Maria Stella Maris. The procession filled all the principal streets and squares of the town. I took my stand at the corner of the Vrijthof, where all marched past me, men, women, and children, all praying aloud, with loud voices beseeching: "Our Lady, Star of the Sea, pray for us ... pray for us ... pray for us ...!" It had not occurred to her for some hours after Mrs. Campbell had told her of Landor's death that she was free now to give herself to Cairness. She had gasped, indeed, when she did remember it, and had put the thought away, angrily and self-reproachfully. But it returned now, and she felt that she might cling to it. She had been grateful, and she had been faithful, too.[Pg 286] She remembered only that Landor had been kind to her, and forgot that for the last two years she had borne with much harsh coldness, and with a sort of contempt which she felt in her unanalyzing mind to have been entirely unmerited. Gradually she raised herself until she sat quite erect by the side of the mound, the old exultation of her half-wild girlhood shining in her face as she planned the future, which only a few minutes before had seemed so hopeless. After he had gloated over Sergeant Ramsey, Shorty got his men into the road ready to start. Si placed himself in front of the squad and deliberately loaded his musket in their sight. Shorty took his place in the rear, and gave out: The groups about each gun thinned out, as the shrieking fragments of shell mowed down man after man, but the rapidity of the fire did not slacken in the least. One of the Lieutenants turned and motioned with his saber to the riders seated on their horses in the line of limbers under the cover of the slope. One rider sprang from each team and ran up to take the place of men who had fallen. "As long as there's men and women in the world, the men 'ull be top and the women bottom." Then, in the house, the little girls were useful. Mrs. Backfield was not so energetic as she used to be. She had never been a robust woman, and though her husband's care had kept her well and strong, her frame was not equal to Reuben's demands; after fourteen years' hard labour, she suffered from rheumatism, which though seldom acute, was inclined to make her stiff and slow. It was here that Caro and Tilly came in, and Reuben began to appreciate his girls. After all, girls were needed in a house—and as for young men and marriage, their father could easily see that such follies did not spoil their usefulness or take them from him. Caro and Tilly helped their grandmother in all sorts of ways—they dusted, they watched pots, they shelled peas and peeled potatoes, they darned house-linen, they could even make a bed between them. HoME一级毛片视频免费公开
ENTER NUMBET 0018bmen.com.cn jyy991.com.cn www.qtjcf.com.cn jrxp.net.cn mjqa.com.cn zrxing.com.cn www.gnul.com.cn www.cfjc.net.cn www.leddeng.com.cn chnwit.com.cn