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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi,

    What version of Qt are you using ?
    On what platform ?

    Can you provide a minimal compilable example that shows that behaviour ?

    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
    0
    • A ajaxcrypto

      I have created multiple QPushButton which go inside a QHBoxLayout. The style sheet specifies different font-sizes for :hover selector i.e.

      QPushButton { text-align: left; font-size: 9pt; }
      QPushButton:hover { font-size: 10pt; }

      This works fine, however, the text gets clipped on the right side on hover whenever I add with a left alignment (Note: This alignment is a must). layout->addWidget(button, 1, Qt::AlignLeft)

      So I tried setting the alignment on the parent container which leads to the same problem again. How do I remove the clipping? (Note: The alignment has to be left).

      joeQJ Offline
      joeQJ Offline
      joeQ
      wrote on last edited by
      #3

      @ajaxcrypto Hi,friend. Welcome.

      could you show what text of your button ? and maybe the width of text is bigger than button's width.

      It is better to show some code snippet at here. we would clear it well.

      Just do it!

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

        I am using Qt 5.9.0, MSVC 2015 on Windows 10 Enterprise. Sadly it is also producible with MinGW builds

        QFrame frame;
            frame.setLayout(new QHBoxLayout);
            frame.layout()->addWidget(new QPushButton{ "TestButton", &frame });
            frame.layout()->addWidget(new QPushButton{ "TestButton", &frame });
            frame.layout()->addWidget(new QPushButton{ "TestButton", &frame });
            frame.setStyleSheet(
                        "QWidget > QPushButton { padding: 5px; border: none; background-color: white; font-size: 9pt; }"
                        "QWidget > QPushButton:hover { font-size: 10pt; } ");
            frame.show();
        
        mrjjM 1 Reply Last reply
        0
        • A ajaxcrypto

          I am using Qt 5.9.0, MSVC 2015 on Windows 10 Enterprise. Sadly it is also producible with MinGW builds

          QFrame frame;
              frame.setLayout(new QHBoxLayout);
              frame.layout()->addWidget(new QPushButton{ "TestButton", &frame });
              frame.layout()->addWidget(new QPushButton{ "TestButton", &frame });
              frame.layout()->addWidget(new QPushButton{ "TestButton", &frame });
              frame.setStyleSheet(
                          "QWidget > QPushButton { padding: 5px; border: none; background-color: white; font-size: 9pt; }"
                          "QWidget > QPushButton:hover { font-size: 10pt; } ");
              frame.show();
          
          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #5

          @ajaxcrypto

          Hi
          I cannot make it do anything strange. same windows and compiler.
          alt text

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

            0_1511374357893_3fcdcf28-17db-4eef-80ed-f427a6c7f573-image.png

            As you can see, the third button, the first and last 'x' is cutoff on hover. What is the solution?
            Also, another problem, QLabel:hover randomly does not work for some labels....

            1 Reply Last reply
            0
            • A ajaxcrypto

              I have created multiple QPushButton which go inside a QHBoxLayout. The style sheet specifies different font-sizes for :hover selector i.e.

              QPushButton { text-align: left; font-size: 9pt; }
              QPushButton:hover { font-size: 10pt; }

              This works fine, however, the text gets clipped on the right side on hover whenever I add with a left alignment (Note: This alignment is a must). layout->addWidget(button, 1, Qt::AlignLeft)

              So I tried setting the alignment on the parent container which leads to the same problem again. How do I remove the clipping? (Note: The alignment has to be left).

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

              @ajaxcrypto
              Assuming your button is just wide enough to show the text at 9pt, what are you expecting to happen if you hover and it changes to 10pt? Are you expecting the button to resize to accommodate?

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

                I do not know what to expect, should the button auto resize to fit contents? But note that if I have a single button inside a layout, it does resize and the text is visible, the problem only happens with multiple buttons in a Layout.

                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #9

                  Hi
                  Button MUST be big enough for the Hover text size.
                  It will not ever resize to fit the text.

                  The buttons will use the width allowed by the layout.

                  Solution is to make the QFrame bigger. :)

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

                    So how do I make sure that they are big enough? The texts displayed in the button are not necessarily static. Do I every time calculate width using QFontMetrics and set it to bigger one? Also then it is not as flexible anymore i.e. the font size on hover can be anything specified by the user through a stylesheet. So do I have to write my own CSS parser to figure it out?

                    mrjjM 1 Reply Last reply
                    0
                    • A ajaxcrypto

                      So how do I make sure that they are big enough? The texts displayed in the button are not necessarily static. Do I every time calculate width using QFontMetrics and set it to bigger one? Also then it is not as flexible anymore i.e. the font size on hover can be anything specified by the user through a stylesheet. So do I have to write my own CSS parser to figure it out?

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

                      @ajaxcrypto
                      Well you can start with a size for window that can hold longer texts.

                      When using style sheet , there is no way to know what font settings are put in there. (sadly)

                      So if you really need to allow user total freedom then yes you must write some code
                      to check the texts with a given font to see if it will fit.

                      You might be able to subclass QPushButton and make it a bit more automatic that way but
                      im not sure how much it would help.

                      1 Reply Last reply
                      0
                      • 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

                                          • Login

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