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 drawing custom QHeaderView sections
Forum Updated to NodeBB v4.3 + New Features

Issue drawing custom QHeaderView sections

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 375 Views 1 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.
  • qwasder85Q Offline
    qwasder85Q Offline
    qwasder85
    wrote on last edited by
    #1

    I'm having a issue with the drawing logic of my table headers.
    I have the following table:
    alt text
    As you can see, I draw my header texts between columns. This is done by shifting the text's drawing rectangle to the left by half of the text's width.

    // In my HeaderView's paintSection()-function:
    QFontMetrics  fm = fontMetrics();
    QString       text = model()->headerData(in_logical_index, Qt::Horizontal).toString();
    int           text_size = fm.horizontalAdvance(text);
    int           offset = in_logical_index == 0 ? 0 : text_size / 2; // Do not offset the first axis value
    QRect         new_rect = QRect(in_rect.left() - offset, in_rect.top(), text_size, in_rect.height());
    Qt::Alignment alignment = Qt::AlignVCenter;
    
    in_p_painter->drawText(new_rect, alignment, text);
    
    // Then add the last value
    

    It works fine until I move the mouse onto one of the headers. This results in the adjacent section's text being drawn over partially by the header my cursor hovers over (as highlighted on the screenshot).
    I know why this happens, but I can't think of a solution for it.
    Maybe someone here has an idea on how to solve this problem?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      The drawing area is clipped to the header section rectangle, so you just have to paint both numbers - the one for current section and the one for adjacent one.

      qwasder85Q 1 Reply Last reply
      1
      • Chris KawaC Chris Kawa

        The drawing area is clipped to the header section rectangle, so you just have to paint both numbers - the one for current section and the one for adjacent one.

        qwasder85Q Offline
        qwasder85Q Offline
        qwasder85
        wrote on last edited by qwasder85
        #3

        @Chris-Kawa I tried that, it results in the second number being parted in the center where both rectangles meet.

        Edit: Now that I think about it, I might be able to enlarge the drawing rectangle to span the gap. Thanks.

        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