Results 1 to 2 of 2

Thread: Vertical label on table horizontal header

  1. #1
    Join Date
    Nov 2012
    Posts
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Vertical label on table horizontal header

    Hi,

    I am trying to have the text on a horizontal header displayed vertically (i.e. rotated 90°).
    I tried the implementation given here, but there are several issues. Here they are, ordered by increasing gravity:


    1. The vertical header is modified, not the horizontal one. This bug is easy to fix, we just need to replace line 12 with:
      Qt Code:
      1. if (!hv || hv->orientation() != Qt::Horizontal)
      To copy to clipboard, switch view to plain text mode 
    2. Even though I tried applying my new style only to a particular table (myTable->setStyle(myProxyStyle)), all the tables in the application are modified. How can I avoid that?

    3. Last but not least, the height of the horizontal header is not adjusted to take into account the size of the label. As a result, the text is truncated (and thus mostly useless). I tried two approaches to solve this problem:
      • override QProxyStyle::sizeForContents(). I expected that it would be called for ContentsType CT_HeaderSection, but unfortunately it is not (it is however called for each view item, which doesn't help).
      • override QProxyStyle::subControlRect(). I hoped that it would be called for SubControl SE_HeaderLabel, but it is not.

      Does anyone have a suggestion about how to solve this problem?


    Note: I'm using Qt 4.8.

  2. The following user says thank you to ipkiss for this useful post:

    davethomaspilot (11th February 2014)

  3. #2
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Vertical label on table horizontal header

    Old thread, I know. I'm interested in the same thing.


    For your 2) I set the style on the specific tableView like this:

    ui->tableView->setStyle(new MyStyle(ui->tableView->style()));

    I did this before assigned the view to the model:

    ui->tableView->setModel(model);


    On 1)
    Thanks for pointing out the need to change if (!hv || hv->orientation() != Qt::Horizontal). My horizontal headers weren't changing at all, and I just gave up.

    I still have the same issue 3--working it now.

    I really like to rotate header text, since frequently the header is wider than any of the column contents for columns that contain short data items (e.g single characters, booleans, etc). With rotated headers, I can then size each column of the table for the maximum expected data size, rather than the width required to see the label.

    A workaround for 3) that works for me is to set the minimum height of the column:

    ui->tableView->horizontalHeader()->setMinimumHeight(150);

    Also, I wanted the text to read from bottom to top,not top to bottom. So, I used -90 for the rotate and changed the painter::translate to use bottomLeft. Then I decided I like -45 better than -90. Here's what I ended up with:

    Qt Code:
    1. void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter,
    2. const QWidget *widget = 0) const
    3. {
    4. if (element == QStyle::CE_HeaderLabel) {
    5. const QHeaderView *hv = qobject_cast<const QHeaderView *>(widget);
    6. if (!hv || hv->orientation() != Qt::Horizontal)
    7. return QProxyStyle::drawControl(element, option, painter, widget);
    8. const QStyleOptionHeader *header = qstyleoption_cast<const QStyleOptionHeader *>(option);
    9. painter->save();
    10. // painter->translate(header->rect.topLeft());
    11. painter->translate(header->rect.bottomLeft());
    12.  
    13. painter->rotate(-45);
    14. painter->drawText(0,0,header->text);
    15. painter->restore();
    16. return;
    17. }
    18. return QProxyStyle::drawControl(element, option, painter, widget);
    19. }
    20. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by davethomaspilot; 11th February 2014 at 20:28.

Similar Threads

  1. Replies: 3
    Last Post: 20th January 2011, 14:24
  2. How to get vertical/horizontal zoom box?
    By knicewar in forum Qwt
    Replies: 3
    Last Post: 23rd March 2010, 05:09
  3. How to set two rows in a table view Horizontal header?
    By sivollu in forum Qt Programming
    Replies: 11
    Last Post: 29th April 2009, 05:57
  4. How to customize horizontal header (diagonal header view)
    By vairamuthu.g in forum Qt Programming
    Replies: 4
    Last Post: 4th September 2008, 16:59
  5. Showing Icon in vertical header of a table
    By vishal.chauhan in forum Qt Programming
    Replies: 7
    Last Post: 15th January 2007, 11:44

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.