Results 1 to 8 of 8

Thread: QHeaderView -- diagonal headers?

  1. #1
    Join Date
    Dec 2007
    Location
    Austin, TX
    Posts
    43
    Thanks
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Wink QHeaderView -- diagonal headers?

    My project has a requirement to have diagonal headers on a few tables, allowing long labels to not take up more horizontal space. I found http://www.qtcentre.org/forum/f-qt-p...view-2282.html
    but when I subclass the QHeaderView & reimplement paintSection I have only the first label diagonal -- the 2nd label is missing. And the section pieces are not rotated to match.

    Qt Code:
    1. class DiagTableHeader : public QHeaderView
    2. {
    3. Q_OBJECT
    4. public:
    5. DiagTableHeader(QWidget* parent = 0);
    6. virtual ~DiagTableHeader() {};
    7.  
    8. int sectionSizeHint(int logicalIndex) const;
    9. QSize sizeHint() const;
    10.  
    11. private:
    12. virtual void paintSection(QPainter* painter, const QRect& rect, int logicalIndex) const;
    13. };
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. DiagTableHeader::DiagTableHeader(QWidget* parent) : QHeaderView(Qt::Horizontal, parent)
    2. {
    3. }
    4.  
    5. int DiagTableHeader::sectionSizeHint(int logicalIndex) const
    6. {
    7. QSize size = ((QTableWidget*)parentWidget())->horizontalHeaderItem(logicalIndex)->sizeHint();
    8. int diag = sqrt(pow(size.height(), 2) + pow(size.width(), 2));
    9. return diag;
    10. }
    11.  
    12. QSize DiagTableHeader::sizeHint() const
    13. {
    14. QSize size = QHeaderView::sizeHint();
    15. int diag = sqrt(pow(size.height(), 2) + pow(size.width(), 2));
    16. size.setHeight(diag); // leave the width the same right now
    17. return size;
    18. }
    19.  
    20. void DiagTableHeader::paintSection(QPainter* painter, const QRect& rect, int logicalIndex) const
    21. {
    22. painter->rotate(-45);
    23. QTableWidgetItem* widget = ((QTableWidget*)parentWidget())->horizontalHeaderItem(logicalIndex);
    24. opt.initFrom(this->viewport());
    25. QStylePainter sect(this->viewport());
    26. sect.rotate(-45);
    27. sect.drawControl(QStyle::CE_HeaderSection, opt); // this doesn't seem to do anything
    28. painter->drawText(rect.left(), rect.bottom(), widget->text());
    29. }
    To copy to clipboard, switch view to plain text mode 

    Note that the 1 label that does show is shifted to the right (even though rect.left() = 0) and up .

    Is there something obvious that I'm missing? Is this even possible?

    Thanks for whatever help you can give me.

    Vycke

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QHeaderView -- diagonal headers?

    Qt Code:
    1. sect.drawControl(QStyle::CE_HeaderSection, opt)
    To copy to clipboard, switch view to plain text mode 
    This won't do anything as your opt object doesn't contain any useful information about the section. Initalizing the object is not enough, you have to fill it with data.

  3. #3
    Join Date
    Dec 2007
    Location
    Austin, TX
    Posts
    43
    Thanks
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Re: QHeaderView -- diagonal headers?

    Quote Originally Posted by wysota View Post
    Qt Code:
    1. sect.drawControl(QStyle::CE_HeaderSection, opt)
    To copy to clipboard, switch view to plain text mode 
    This won't do anything as your opt object doesn't contain any useful information about the section. Initalizing the object is not enough, you have to fill it with data.
    Ok... Changed that opt to
    Qt Code:
    1. opt.initFrom(this->viewport();
    2. opt.text = widget->text();
    3. opt.section = logicalIndex;
    4. if (logicalIndex > 0)
    5. opt.position = QStyleOptionHeader::Middle;
    To copy to clipboard, switch view to plain text mode 

    and added
    Qt Code:
    1. sect.translate(-rect.left(), rect.bottom()/2);
    To copy to clipboard, switch view to plain text mode 
    before the Section drawControl
    and
    Qt Code:
    1. sect.translate(-rect.left(), rect.bottom()/2);
    2. sect.drawControl(QStyle::CE_HeaderLabel, opt);
    To copy to clipboard, switch view to plain text mode 
    after the Section draw and removed the drawText().

    The translates help place the controls where they belong... but it appears that the whole header is rotated, not just the sections inside it.

    Any further clues? Or is what I'm trying actually impossible?

    Vycke

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QHeaderView -- diagonal headers?

    Honestly I have no idea what you are trying to obtain, so it's hard to help in any way.

  5. #5
    Join Date
    Dec 2007
    Location
    Austin, TX
    Posts
    43
    Thanks
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHeaderView -- diagonal headers?

    Ok.. here's what the "requirements" look like (see attachment)

    Vycke
    Attached Images Attached Images

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QHeaderView -- diagonal headers?

    In that case rotating the painter while drawing will certainly not be enough...

  7. #7
    Join Date
    Dec 2007
    Location
    Austin, TX
    Posts
    43
    Thanks
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHeaderView -- diagonal headers?

    Yeah Kinda figured that out. The real question, I guess, is "IS it possible?" I might just tell the guys doing the req's that they get horizontal text or vertical text, but that diagonal stuff is "right out"

    Vycke

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QHeaderView -- diagonal headers?

    Everything is possible. It's just a matter of asking oneself if the result is worth the effort...

Similar Threads

  1. deleting selected headers
    By ru_core in forum Qt Programming
    Replies: 3
    Last Post: 16th April 2008, 07:53
  2. Renumbering Row Headers of QHeaderView
    By mclark in forum Newbie
    Replies: 4
    Last Post: 11th September 2006, 15:04

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.