How can I create the horizontal headers at a 45 degree angle
Unsolved
General and Desktop
-
Hello to all,
how can I create a table with the horizontal headers tilted by 45 degrees as in the following figure:
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:
what's wrong? please help me. thank you so much
-
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.