Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Issue with QPushButton text horizontal clipping
Forum Updated to NodeBB v4.3 + New Features

Issue with QPushButton text horizontal clipping

Scheduled Pinned Locked Moved Unsolved General and Desktop
31 Posts 5 Posters 10.2k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ajaxcrypto
    wrote on last edited by ajaxcrypto
    #12

    So for an auto resize-able button, I have to write a QSS parser to figure out the font size on various selectors, then use this to change the button dimension based on that and the current text set for the button? (Because the user can specify this:

    QPushButton { font-size: 10pt; }
    QPushButton:hover { font-size: 11pt; }
    QPushButton:pressed { font-weight: bold; }
    // numerous other such combinations with properties/selectors...
    

    That seems like an awful amount of work for something seemingly simple. Also, when should I parse the stylesheet if I override a QPushButton ?

    1. When it is created with a text and parent, get parent style sheet and parse
    2. When setStyleSheet is called
    3. When setText is called
    4. Probably at resize event as well ?
    5. When setObjectName is called

    Note: The stylesheet can be set anywhere, either the button itself, or any parent or application wide.

    Edit: Also, I need to combine multiple such style blocks when they are set at multiple levels and match the QPushButton and figure out which has the highest specificity and then calculate the dimension based on that...

    mrjjM 1 Reply Last reply
    0
    • A ajaxcrypto

      So for an auto resize-able button, I have to write a QSS parser to figure out the font size on various selectors, then use this to change the button dimension based on that and the current text set for the button? (Because the user can specify this:

      QPushButton { font-size: 10pt; }
      QPushButton:hover { font-size: 11pt; }
      QPushButton:pressed { font-weight: bold; }
      // numerous other such combinations with properties/selectors...
      

      That seems like an awful amount of work for something seemingly simple. Also, when should I parse the stylesheet if I override a QPushButton ?

      1. When it is created with a text and parent, get parent style sheet and parse
      2. When setStyleSheet is called
      3. When setText is called
      4. Probably at resize event as well ?
      5. When setObjectName is called

      Note: The stylesheet can be set anywhere, either the button itself, or any parent or application wide.

      Edit: Also, I need to combine multiple such style blocks when they are set at multiple levels and match the QPushButton and figure out which has the highest specificity and then calculate the dimension based on that...

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #13

      @ajaxcrypto
      Hi
      I would not try to parse the stylesheet as that would just be so involved.

      What i imagine is something like ( using the QStyleOption for push button)

       void CustomWidget::paintEvent(QPaintEvent *)
       {
           QStyleOption opt;
           opt.init(this); // any font set by style sheet should be reflected here
          // calculate text size using QFontMetrics
         // set minimumWidth based on that.
           QPainter p(this);
           style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); // would use the  style()->drawButton or what ever is used for pushbuttons
      

      I have not checked that QStyleOptionXXX does in fact reflect the font but since the above code
      lets me use custom widgets with stylesheet so I assume it will be same for a QPushButton child.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        ajaxcrypto
        wrote on last edited by
        #14

        Here is the paint event implementation from QPushButton:

        void QPushButton::paintEvent(QPaintEvent *)
        {
            QStylePainter p(this);
            QStyleOptionButton option;
            initStyleOption(&option);
            p.drawControl(QStyle::CE_PushButton, option);
        }
        

        It already uses the QStyleOptionButton ...

        mrjjM 1 Reply Last reply
        0
        • A ajaxcrypto

          Here is the paint event implementation from QPushButton:

          void QPushButton::paintEvent(QPaintEvent *)
          {
              QStylePainter p(this);
              QStyleOptionButton option;
              initStyleOption(&option);
              p.drawControl(QStyle::CE_PushButton, option);
          }
          

          It already uses the QStyleOptionButton ...

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #15

          @ajaxcrypto
          Yes ofc. ( i just showed the concept with QWidget and not the style used for button)
          So if u subclass it and add some size checking, it might fly ?

          1 Reply Last reply
          0
          • A Offline
            A Offline
            ajaxcrypto
            wrote on last edited by
            #16

            But how do I figure out the required max height/width? For that the logic is

            1. Parse and resolve all stylesheet
            2. Figure out the largest dimensions based on font-size and font-weight
            3. Resize the button accordingly.

            Step 2 would require like looping through all the selector/combinators applied to the button and figuring out the largest font size and weight

            mrjjM 1 Reply Last reply
            0
            • A ajaxcrypto

              But how do I figure out the required max height/width? For that the logic is

              1. Parse and resolve all stylesheet
              2. Figure out the largest dimensions based on font-size and font-weight
              3. Resize the button accordingly.

              Step 2 would require like looping through all the selector/combinators applied to the button and figuring out the largest font size and weight

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #17

              @ajaxcrypto
              QStyle respect stylesheets.
              So i would not look at the stylesheet as text but when its been processed by Qt.
              At the moment of painting.
              That would be in the QStyleOptionButton.
              So with a little luck, you can make button check the actual text size and
              adjust if needed.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                ajaxcrypto
                wrote on last edited by
                #18

                This fiddle nicely demonstrates what I wanted to acheive with QPushButton

                https://jsfiddle.net/h7fkj5ow/

                mrjjM 1 Reply Last reply
                0
                • A Offline
                  A Offline
                  ajaxcrypto
                  wrote on last edited by
                  #19

                  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 a QPushButton i.e.

                  button->setStyleProperty("hover", "background-color", Qt::white);

                  mrjjM 1 Reply Last reply
                  0
                  • A ajaxcrypto

                    This fiddle nicely demonstrates what I wanted to acheive with QPushButton

                    https://jsfiddle.net/h7fkj5ow/

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #20

                    @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.

                    1 Reply Last reply
                    0
                    • A ajaxcrypto

                      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 a QPushButton i.e.

                      button->setStyleProperty("hover", "background-color", Qt::white);

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #21

                      @ajaxcrypto
                      Hi
                      They consider the parser private.
                      I also dreamed of an API so colors etc could be read from stylesheet alone.

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        ajaxcrypto
                        wrote on last edited by
                        #22

                        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.

                        mrjjM 1 Reply Last reply
                        0
                        • A ajaxcrypto

                          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.

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by mrjj
                          #23

                          @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.

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #24

                            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.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            1
                            • A Offline
                              A Offline
                              ajaxcrypto
                              wrote on last edited by
                              #25

                              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.

                              1 Reply Last reply
                              0
                              • JonBJ Online
                                JonBJ Online
                                JonB
                                wrote on last edited by JonB
                                #26

                                @ajaxcrypto

                                [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.

                                1 Reply Last reply
                                0
                                • A Offline
                                  A Offline
                                  ajaxcrypto
                                  wrote on last edited by
                                  #27

                                  @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.

                                  JonBJ 1 Reply Last reply
                                  0
                                  • A ajaxcrypto

                                    @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.

                                    JonBJ Online
                                    JonBJ Online
                                    JonB
                                    wrote on last edited by
                                    #28

                                    @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?

                                    1 Reply Last reply
                                    0
                                    • A Offline
                                      A Offline
                                      ajaxcrypto
                                      wrote on last edited by
                                      #29

                                      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).

                                      1 Reply Last reply
                                      0
                                      • A Offline
                                        A Offline
                                        ajaxcrypto
                                        wrote on last edited by
                                        #30

                                        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"?

                                        mrjjM 1 Reply Last reply
                                        0
                                        • A ajaxcrypto

                                          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"?

                                          mrjjM Offline
                                          mrjjM Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #31

                                          @ajaxcrypto
                                          Hi leave open a bit, some are in other time zones so not all hope is lost yet :)

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved