Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QHeaderView with vertical label



  • 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.


  • Lifetime Qt Champion

    Hi,

    Are you sure you are painting inside the right rectangle ?



  • 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.


  • Lifetime Qt Champion

    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 :)


Log in to reply