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. QHeaderView with vertical label
Forum Update on Monday, May 27th 2025

QHeaderView with vertical label

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 961 Views
  • 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.
  • D Offline
    D Offline
    daviraim
    wrote on last edited by
    #1

    Dear forum,
    I would like to rotate horizontal header labels of a QTableWidget. I tried to override the paintSection as stated in several topics but the result is not correct. First, the paintSection function is only called for the first logicalIndex (column 0) even if I have several column. When I run my code, only the label of the first column label is therefore rotated & displayed. Moreover with this paintSection override, when I dynamically add rows to the QTableWidget (with setRowCount(ui->tableWidget->rowCount()+1)), my example creates as many columns as there are rows in the defined QTableWidget. It does not happen when I do not overload the class QHeaderView. I have a little trouble to understand these behaviors.
    Any ideas ? Maybe there is another way to rotate the header labels...
    Thanks for your suggestions.

    Here is my paintSection function :

    void BRotatedHeaderView::paintSection(QPainter* painter, const QRect& rect, int logicalIndex) const
    {
    painter->save();
    QHeaderView::paintSection(painter, rect, logicalIndex);
    painter->restore();
    painter->setRenderHint(QPainter::Antialiasing);

    QString title = model()->headerData(logicalIndex,Qt::Horizontal,Qt::DisplayRole).toString();
    painter->translate(rect.x(), rect.y());
    painter->rotate(90);
    painter->drawText(0,0, title);
    
    qDebug() << __PRETTY_FUNCTION__ << title;
    

    }

    Regards.
    David.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Are you sure you are painting inside the right rectangle ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • D Offline
        D Offline
        daviraim
        wrote on last edited by
        #3

        Thanks Samuel. Your remark helped me a lot. In fact, the BRotatedHeaderView override class was initialized with the wrong Qt::Orientation. It works now. Regards.

        1 Reply Last reply
        1
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Great !

          Since you have it working now, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          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