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. How can I create the horizontal headers at a 45 degree angle
Qt 6.11 is out! See what's new in the release blog

How can I create the horizontal headers at a 45 degree angle

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

    Hello to all,

    how can I create a table with the horizontal headers tilted by 45 degrees as in the following figure:

    30b19bb5-5727-4bb9-98ec-3d9f06bf0626-image.png

    I entered as the following codes for paintSection of the HeaderView:

    //a header view that renders text vertically
    class HorizontallHeaderView : public QHeaderView
    {
    public:
        explicit HorizontallHeaderView(Qt::Orientation orientation, QWidget *parent = nullptr)
            : QHeaderView(orientation, parent) {}
    
        void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const override
        {
            QPointF rectCenter = QRectF(rect).center();
            painter->save();
            //rotate around rectCenter
            painter->translate(rectCenter.x(), rectCenter.y());
            painter->rotate(-45.0);
            painter->translate(-rectCenter.x(), -rectCenter.y());
            //apply the same transformation on the rect
            QRect rectCopy = painter->worldTransform().mapRect(rect);
            //use base paintSection method after applying required transformations
            QHeaderView::paintSection(painter, rectCopy, logicalIndex);
            painter->restore();
        }
    
        QSize sectionSizeFromContents(int logicalIndex) const override
        {
            //get sizeHint from base sizeHint method
            QSize val = QHeaderView::sectionSizeFromContents(logicalIndex);
            //swap height and width
            return QSize(val.height(), val.width());
        }
    };
    

    main.cpp

    HorizontallHeaderView *horizontalHeaderView = new HorizontallHeaderView(Qt::Horizontal);
    
    tableWidget->clear();
    tableWidget->setRowCount(4);
    tableWidget->setColumnCount(8);
    tableWidget->verticalHeader()->setBaseSize(QSize(30,10));
    tableWidget->horizontalHeader()->setBaseSize(QSize(30,10));
    tableWidget->setHorizontalHeader(horizontalHeaderView);
    tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    tableWidget->setHorizontalHeaderLabels(setHeaderHorizontalLabels());
    tableWidget->setVerticalHeaderLabels(setHeaderVerticalLabels());
    
    
    QStringList TableWidgetWindow::setHeaderVerticalLabels()
    {
        QStringList m_itemListHeaderVertical;
        m_itemListHeaderVertical.append("Location 1 \n ...");
        m_itemListHeaderVertical.append("Location 2 \n ...");
        m_itemListHeaderVertical.append("Location 3 \n ...");
        m_itemListHeaderVertical.append("Location 4 \n ...");
    
        return m_itemListHeaderVertical;
    }
    
    QStringList TableWidgetWindow::setHeaderHorizontalLabels()
    {
        QStringList m_itemHeaderHorizontal;
        m_itemHeaderHorizontal.append("Column 1");
        m_itemHeaderHorizontal.append("Column 2");
        m_itemHeaderHorizontal.append("Column 3");
        m_itemHeaderHorizontal.append("Column 4");
        m_itemHeaderHorizontal.append("Column 5");
        m_itemHeaderHorizontal.append("Column 6");
        m_itemHeaderHorizontal.append("Column 7");
        m_itemHeaderHorizontal.append("Column 8");
    
        return m_itemHeaderHorizontal;
    }
    

    but it is not tilted well at 45 degrees, it looks like in the following figure:

    ac26b326-1db2-460e-af3a-0de893b472d4-image.png

    what's wrong? please help me. thank you so much

    D 1 Reply Last reply
    0
    • D Domenico

      Hello to all,

      how can I create a table with the horizontal headers tilted by 45 degrees as in the following figure:

      30b19bb5-5727-4bb9-98ec-3d9f06bf0626-image.png

      I entered as the following codes for paintSection of the HeaderView:

      //a header view that renders text vertically
      class HorizontallHeaderView : public QHeaderView
      {
      public:
          explicit HorizontallHeaderView(Qt::Orientation orientation, QWidget *parent = nullptr)
              : QHeaderView(orientation, parent) {}
      
          void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const override
          {
              QPointF rectCenter = QRectF(rect).center();
              painter->save();
              //rotate around rectCenter
              painter->translate(rectCenter.x(), rectCenter.y());
              painter->rotate(-45.0);
              painter->translate(-rectCenter.x(), -rectCenter.y());
              //apply the same transformation on the rect
              QRect rectCopy = painter->worldTransform().mapRect(rect);
              //use base paintSection method after applying required transformations
              QHeaderView::paintSection(painter, rectCopy, logicalIndex);
              painter->restore();
          }
      
          QSize sectionSizeFromContents(int logicalIndex) const override
          {
              //get sizeHint from base sizeHint method
              QSize val = QHeaderView::sectionSizeFromContents(logicalIndex);
              //swap height and width
              return QSize(val.height(), val.width());
          }
      };
      

      main.cpp

      HorizontallHeaderView *horizontalHeaderView = new HorizontallHeaderView(Qt::Horizontal);
      
      tableWidget->clear();
      tableWidget->setRowCount(4);
      tableWidget->setColumnCount(8);
      tableWidget->verticalHeader()->setBaseSize(QSize(30,10));
      tableWidget->horizontalHeader()->setBaseSize(QSize(30,10));
      tableWidget->setHorizontalHeader(horizontalHeaderView);
      tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
      tableWidget->setHorizontalHeaderLabels(setHeaderHorizontalLabels());
      tableWidget->setVerticalHeaderLabels(setHeaderVerticalLabels());
      
      
      QStringList TableWidgetWindow::setHeaderVerticalLabels()
      {
          QStringList m_itemListHeaderVertical;
          m_itemListHeaderVertical.append("Location 1 \n ...");
          m_itemListHeaderVertical.append("Location 2 \n ...");
          m_itemListHeaderVertical.append("Location 3 \n ...");
          m_itemListHeaderVertical.append("Location 4 \n ...");
      
          return m_itemListHeaderVertical;
      }
      
      QStringList TableWidgetWindow::setHeaderHorizontalLabels()
      {
          QStringList m_itemHeaderHorizontal;
          m_itemHeaderHorizontal.append("Column 1");
          m_itemHeaderHorizontal.append("Column 2");
          m_itemHeaderHorizontal.append("Column 3");
          m_itemHeaderHorizontal.append("Column 4");
          m_itemHeaderHorizontal.append("Column 5");
          m_itemHeaderHorizontal.append("Column 6");
          m_itemHeaderHorizontal.append("Column 7");
          m_itemHeaderHorizontal.append("Column 8");
      
          return m_itemHeaderHorizontal;
      }
      

      but it is not tilted well at 45 degrees, it looks like in the following figure:

      ac26b326-1db2-460e-af3a-0de893b472d4-image.png

      what's wrong? please help me. thank you so much

      D Offline
      D Offline
      Domenico
      wrote on last edited by
      #2

      @Domenico

      hello everyone, there is no solution to the problem described in the previous post?

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

        Hi,

        Please show some patience and allow at least 24 hours before bumping your own thread. People answering here do it on their own time and might not live in the same time zone as you.

        As for your issue, you are just rotating the original painting. What you want is to draw a parallelogram with the text in it. You will to do the drawing yourself.

        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

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved