Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-138495

QCommonStyle subElementRect HeaderLabel QRect size

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • Not Evaluated
    • None
    • 6.10.0 Beta1
    • Widgets: Styles
    • None
    • Windows 11 with dark fusion theme, tested with Qt 6.8.1
      (with default light windows theme this issue does not occur)
    • All
    • All

    Description

      In QCommonStyle::subElementRect() the header is handled in multiple parts, Section, Label and Indicator in general.

      Regarding the rect size of the HeaderLabel, it is done as follows.

      QCommonStyle SE_HeaderLabel

          case SE_HeaderLabel: {
              int margin = proxy()->pixelMetric(QStyle::PM_HeaderMargin, opt, widget);
              r.setRect(opt->rect.x() + margin, opt->rect.y() + margin,
                        opt->rect.width() - margin * 2, opt->rect.height() - margin * 2);
      
              if (const QStyleOptionHeader *header = qstyleoption_cast<const QStyleOptionHeader *>(opt)) {
                  // Subtract width needed for arrow, if there is one
                  if (header->sortIndicator != QStyleOptionHeader::None) {
                      if (opt->state & State_Horizontal)
                          r.setWidth(r.width() - (opt->rect.height() / 2) - (margin * 2));
                      else
                          r.setHeight(r.height() - (opt->rect.width() / 2) - (margin * 2));
                  }
              }
              r = visualRect(opt->direction, opt->rect, r);
              break; }
      

      The confusion is the logic behind the setWidth or setHeight for the Header Label part. Those calculations above don't make sense. The half of the Header Section size has nothing to with the size of the Header Indicator part nor with the Header Label part overall.

      As an example, you can see that the text is cropped incorrectly (hence, the rect size is wrong):

       

      Instead, the logic of QStyleSheetStyle could be used which correctly calculates the HeaderLabel rect size.

      QStyleSheetStyle SE_HeaderLabel

          case SE_HeaderLabel: {
              QRenderRule subRule = renderRule(w, opt, PseudoElement_HeaderViewSection);
              if (subRule.hasBox() || !subRule.hasNativeBorder()) {
                  auto r = subRule.contentsRect(opt->rect);
                  if (const QStyleOptionHeader *header = qstyleoption_cast<const QStyleOptionHeader *>(opt)) {
                      // Subtract width needed for arrow, if there is one
                      if (header->sortIndicator != QStyleOptionHeader::None) {
                          const auto arrowRect = subElementRect(SE_HeaderArrow, opt, w);
                          if (arrowRect.isValid()) {
                              if (opt->state & State_Horizontal)
                                  r.setWidth(r.width() - arrowRect.width());
                              else
                                  r.setHeight(r.height() - arrowRect.height());
                          }
                      }
                  }
                  return r;
              }
      

      I tested it and it works for me. I just modified the setWidth and setHeight calculation.

       

      Important Note: the issue occurs only if the headerlabel is bigger (higher) than 1 line. With a single line text it does not show the cropping. But if the rect height is increased (e.g. the header has text plus a QLineEdit widget in it) or there is multiple line of text, the cropping is there.

       

      Additional details in Qt Forum

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            tpochep Timur Pocheptsov
            rtommy Tamas Regos
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes