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

QHeaderView::sectionResizeMode not applied changing font size, family, etc.



  • Hello, all.

    If the font size of QHeaderView's sections is increased either through QSS:

    QHeaderView::section:horizontal {
        font-size: 14pt;
    }
    

    or by means of C++:
    QWidget::setFont

    setting the QHeaderView::sectionResizeMode to QHeaderView::ResizeToContents
    doesn't fit the contents of the whole column correctly - the header's title is cut either from one or from both sides. The contents are fit correctly only when the font size matches some standard one - presumably, the application one. If the font size - be it the pixel or point one, is greater than this reference value, cutting takes place. Made a QStyledItemDelegate subclass and overrode the QStyledItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index), so that to dump the available fonts. Neither the cell's font option.font nor that of the whole header option.widget->font() had the requested font size and, presumably, this is the reason for the incorrect size hint calculation. Looked at the sources of QStyledItemDelegate, QCommonStyle, QWindowsStyle, etc. for the way the size hint is calculated but to figure out the exact place and reason why the correct font is not taken into consideration would take time.

    Any suggestions are welcome how to make QTableView or QTreeView correctly take into consideration the font when calculating the size hint.

    Another similar issue is the positioning of the sort indicator relatively to the rest of the cell's contents. If the sort indicator is positioned on the right:

    QHeaderView::up-arrow,
    QHeaderView::down-arrow {
        position: right;
    }
    

    it overlaps the rest of the content if there isn't enough space to the right. This is usually the text. An interesting peculiarity is that setting its position to left or bottom results in positioning it to the right.

    Any suggestions here how to fix this as well using some standardized approach are welcome as well. The brute force one is known - draw it yourself and calculate the size hint yourself.


  • Qt Champions 2019

    What Qt version do you use? Afaik this should be fixed in 5.11: https://bugreports.qt.io/browse/QTBUG-56457?jql=text ~ "qheaderview font-size"
    btw: Searching in the bug tracker before writing such a long text would have saved you some time I guess :)



  • What Qt version do you use?

    Qt 5.10.1.

    Afaik this should be fixed in 5.11: https://bugreports.qt.io/browse/QTBUG-56457?jql=text ~ "qheaderview font-size"

    In vain, using Qt 5.11 it isn't:
    0_1535616823663_111e697f-dffe-4079-9b3f-905752454226-image.png

    Searching in the bug tracker before writing such a long text would have saved you some time I guess :)

    Correct. I've only searched in the forum and Google but neither provided the desired result, presumably due to not well composed query. Thought of filing it as a bug but decided to ask first.



  • Let's try to nail where the problem is. Try using a QStandardItemModel in a tableview and use QStandardItemModel::setHeaderData(i,Qt::Horizontal,yourFont,Qt::FontRole) where yourFont is a QFont to see if it resizes correctly that way.

    Also, if you could provide a minimal example to reproduce it would be very helpful


  • Qt Champions 2019

    @VRonin : I'm pretty sure it's a stylesheet problem. There is a similar bug around in the bugtracker and I think they are all closed in 5.12.
    But yes, we need a minimal example.


Log in to reply