Results 1 to 9 of 9

Thread: Adjusting the text in a header of a QTableView

  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Adjusting the text in a header of a QTableView

    Hello!

    In one of my apps I have a QTableView implemented with a QTableModelView as model. I have the QVariant headerData(...) method reimplemented in my model's definition and it's supposed to retrieve a junction of two QStrings: one containing a given physical quantity and the other, inside "[ ]", its unity, such as in the image below:

    Screenshot from 2014-07-22 15:13:48.png

    The image shows the result of what I have implemented by now. As you can see, what happens is that the name of some physical quantities are bigger then others (or the name of a pq's unity is bigger then others), leading to the situation you see on the picture. But what I would like to have implemented is a formatted pattern where the pq's unity (with the "[ ]") is always at the far right of the header 'label' while only the PQ's name is at the far left.

    How can I implement this?


    Thanks,

    Momergil
    May the Lord be with you. Always.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Adjusting the text in a header of a QTableView

    Derive a class from QHeaderView and use the QTableView::setVerticalHeader() method to install it on your QTableView.

    For your custom QHeaderView class, reimplement the QHeaderView::paintSection() method to format the text the way you want (e.g. one call to QPainter::drawText() for the physical quantity text using a left alignment flag, the other for the unit text with a right alignment flag).

    You can use the QAbstractItemModel::headerData() method to retrieve your text from the model, just as Qt's own QHeaderView does, using QHeaderView::model(). The logicalIndex argument passed into the paintSection() method is the index into the headerData.

  3. The following user says thank you to d_stranz for this useful post:

    Momergil (23rd July 2014)

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Adjusting the text in a header of a QTableView

    After thinking about this overnight, it might also be possible to do this without a custom QHeaderView. You may be able to format the string returned by headerData() as Rich Text, and embed the formatting in that using HTML formatting. I do not know if QHeaderView can accept Rich Text for labels, but I would be sort of surprised if it didn't.

    Edit: No, it doesn't look like this will work. Formatting a header string with "<b>Some text</b>" results in a header that contains "<b>Some text</b>"
    Last edited by d_stranz; 23rd July 2014 at 20:01.

  5. The following user says thank you to d_stranz for this useful post:

    Momergil (29th July 2014)

  6. #4
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Adjusting the text in a header of a QTableView

    Thanks for you replies, d_stranz (and for meditating about this "overnight" :P). I'll try to implement your first solution, then, and return here with further questions if necessary.


    Thanks,

    Momergil
    May the Lord be with you. Always.

  7. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Adjusting the text in a header of a QTableView

    Please post a screen shot when you get it working.

  8. #6
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Adjusting the text in a header of a QTableView

    Quote Originally Posted by d_stranz View Post
    Please post a screen shot when you get it working.
    WHEN I get it working - what is certainly not what I have now :x

    For now I did managed to do what I want, but not properly. If I reimplement the QHeaderView:aintSection() method this way:

    Qt Code:
    1. void paintSection(QPainter* painter, const QRect& rect, int logicalIndex) const
    2. {
    3. const QString strTemp1 = model()->headerData(logicalIndex,Qt::Vertical).toString().section("[",0,0);
    4. const QString strTemp2 = model()->headerData(logicalIndex,Qt::Vertical).toString().section("[",1,1);
    5.  
    6. painter->drawText(rect,Qt::AlignLeft,strTemp1);
    7.  
    8. if (!strTemp2.isEmpty())
    9. painter->drawText(rect,Qt::AlignRight,"[" + strTemp2);
    10.  
    11. QHeaderView::paintSection(painter,rect,logicalIndex);
    12. }
    To copy to clipboard, switch view to plain text mode 

    what I get is nothing different from standart implementation, since the default implementation of that method takes care of hiding the effects of my modifications. Removing the call to QHeaderView:aintSection() show the text formatted as I want, but all else in the header drawing is not drawn:

    Screenshot from 2014-07-30 10:59:55.png

    So it would seem that I need to Ctrl+C / Ctrl+V the original code inside QHeaderView:aintSection() and edit it as needed, but that move has two problems:

    * First, that code calls a private member "d" all the time to see the header's display configuration, and while some of the calling things of "d" can be replaced (e.g.: "d->model" for "model()"), others can't (e.g.: d->clickableSections or d->isSectionSelected(logicalIndex)). At the end I have to comment parts of the code resulting in a partial visualization of the header;
    * Second, the text shown by the default QHeaderView:aintSection() uses a QStyleOptionHeader object that has a single QString to be shown, not two to make the compound image, and if I comment its usage and call QPainter::drawText instead, no text is shown.


    So how to get around this situation? :S
    May the Lord be with you. Always.

  9. #7
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Adjusting the text in a header of a QTableView

    Someone? Help?
    May the Lord be with you. Always.

  10. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Adjusting the text in a header of a QTableView

    Don't call the parent class implementation, because you do not want it drawn that way. You need to work out how the base code draws the cell content including borders, margins, highlighting and mimic that. You cannot, as you note, copy the Qt private code verbatim.

    Have you tried simply putting a tab character between the label and unit, i.e. " label\t[unit]", to see what the default rendering does with that?

    Another approach. It would be quite easy if the header font was fixed width. Then you could make each string, say, 20 chars and construct with the appropriate number of spaces in between label and unit.

    Another approach. Do not display the unit in the label but as a tool tip on the header view cell.

  11. The following user says thank you to ChrisW67 for this useful post:

    Momergil (29th August 2014)

  12. #9
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Adjusting the text in a header of a QTableView

    Quote Originally Posted by ChrisW67 View Post
    Have you tried simply putting a tab character between the label and unit, i.e. " label\t[unit]", to see what the default rendering does with that?
    Yes: it puts a space instead of a tab \o/ (that happens both when using the model's headerData() reimplementation as well as when reimplementing QHeaderView's paintSection().

    Quote Originally Posted by ChrisW67 View Post
    Another approach. It would be quite easy if the header font was fixed width. Then you could make each string, say, 20 chars and construct with the appropriate number of spaces in between label and unit.
    Yeah, I did think about a similar solution, but it was quite inefficient... The only difference is that the header should not be size fixed (the result from using a size-fixed font plus a fixed number of letters on each string), so the calculation was based on largest of strings using a QLabel::width() comparison after calling adjustSize(). So as you can see, quite inefficient and it was abandoned =T

    Quote Originally Posted by ChrisW67 View Post
    Another approach. Do not display the unit in the label but as a tool tip on the header view cell.
    No way: its part of the software specification that the header should be so, and it's for a touchscreen embedded Linux device so tool tips are not welcome
    May the Lord be with you. Always.

Similar Threads

  1. Replies: 21
    Last Post: 13th August 2013, 13:38
  2. QTableview with flat header
    By mqt in forum Qt Programming
    Replies: 3
    Last Post: 22nd June 2013, 12:19
  3. QTableView with fixed header
    By aLiEnHeAd in forum Qt Programming
    Replies: 1
    Last Post: 25th November 2008, 09:14
  4. Replies: 1
    Last Post: 3rd September 2008, 15:16
  5. QTableView header difficulties
    By croftj in forum Qt Programming
    Replies: 6
    Last Post: 30th January 2008, 21:58

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.