Unsolved Issue with QPushButton text horizontal clipping
-
This fiddle nicely demonstrates what I wanted to acheive with
QPushButton
-
Tried fiddling with style option, does not work, in most cases, even the intial text gets clipped horizontally, on hover, clips even more... So basically an auto expanding button which fits to its content is simply not possible without writing a complete QSS parsing system?
Why doesn't Qt expose the parser API ? It would have saved me days worth of work... Like maybe changing a single property for
:hover
selector based on some condition for aQPushButton
i.e.button->setStyleProperty("hover", "background-color", Qt::white);
-
@ajaxcrypto
Hi
I understand it. but none of the widgets will grow to fit text.Sadly, it seems the QStyleOptionButton do not reflect the new
font size set by hover. It reports width as same so my idea was not as straight as i had hoped.
It should be in there some where. -
@ajaxcrypto
Hi
They consider the parser private.
I also dreamed of an API so colors etc could be read from stylesheet alone. -
So basically I have to roll my own QSS parser, keep track of all relevant styles and then paint widgets accordingly for auto-resizing widgets based on current properties... Sigh
Might as well write my own GUI library at this stage using primitives + signal/slots...Note: I could not find any documentation as to where selector specific style information is stored, neither are there public/protected member functions to get them.
-
@ajaxcrypto
Parsing stylesheet would be my last attempt.
QStyle, QProxyStyle and friends provides many ways to alter
how stylesheets are used.My idea with QStyleOptionButton almost worked except I cant find where the
hover font size is stored. Not even the font for painter seems to change which i find a bit odd.
(update: my idea with QStyleOptionButton cannot be used. not reflected. sorry)Before you go down the long and evil road, give it some days and see if someone has a good idea
on how to do it without parsing the actual stylesheet. -
What about using the QEvent::HoverEnter and QEvent::HoverLeave events to modify the font ?
You could also use QFontMetrics to calculate the minimum size needed to fit the text in the button. -
There are other selectors as well like
:pressed
, so I have to do this for all of them and their combinations when set with property value i.e.QPushButton[prop="value"]:hover { ... }
QPushButton[prop="other"]:hover { ... }
I wanted a generic solution which works for all kinds of stylesheet, not a particular stylesheet.
-
[ASIDE: All this stuff about parsing stylesheets, that's what I was talking about in https://forum.qt.io/topic/85184/undoing-an-added-setstylesheet-property, wanting individual properties to be accessible. But that's how it is at present. You cannot access specific selectors.]
You probably won't thank me for this comment. I don't know what your application is all about. But do you really have a requirement to support anyone making any arbitrary changes they fancy to buttons in the stylesheet and then having to ensure that whatever fits in the button? Then it seems likely you'll want to allow arbitrary styling of any element, so sorting out the dynamic sizing of a button will only be the start of what you'll face? Is the amount of effort required worth it for the return? My 2 cents.
-
@JNBarchan Style sheets are not completely arbitrary, but the code I am writing is more of a library which will be used in multiple applications (not tied to any particular application), so yes, I have very limited control over what kind of style sheets I will get.
-
@ajaxcrypto
What about the practicality that button font size is only the start, presumably you want to allow loads of other widgets/layouts to be alterable? -
Note that what I asked here is merely a single example of a property, but is mostly limited to colors, borders and font properties. I already have some custom widgets (about a dozen) of them ready, so yes, direct access to properties from style sheet would be great. The number of widgets which I want this on is however quite limited (not arbitrary). The bunch of applications will have some common feel and some feel unique to themselves (like a family).
-
Thank you everyone! I think I understand the challenges of implementing the feature set I need. I may pursue either parsing or have style sheet templates (if at all possible). For now, will focus on other things instead. Should I keep this thread "Unsolved"?
-
@ajaxcrypto
Hi leave open a bit, some are in other time zones so not all hope is lost yet :)