Qt World Summit: Submit your Presentation

Resized QTreeWidget headers hiding items

  • Hi,

    I've got a QTreeWidget and I want to make a (horizontal) header take up lots more vertical space on a signal; I can resize it fine using tree->header()->setMinimumSize etc, but when I do the items beneath stay in the same places, so the first 8 or so entries are hidden beneath it. How can I move them all downwards? Is there a better function for resizing the header (there's only one column), or is there any way to adjust vertical position of the items?

    Thanks in advance!

  • Maybe you should do the following:

    If you use Qt Designer (same could be done with code)
    Put QTextEdit above QTreeView and assign vertical layout for the parent container.

    "Put a lot of text" in the QTextBox and resize it as needed.

    It will resize (push down) your QTreeView and QTreeView will keep its good shape.

  • The view uses QHeader::sizeHint() instead of the actual size of the header to draw itself, and QHeader::sizeHint() doesn't take minimumSize() into account.

    You can fix that by deriving QHeader, reimplementing sizeHint(), and calling updateGeometries on the tree after each call to setMinimumSize to manually force the view update.

    @class HeaderView : public QHeaderView {
    explicit HeaderView(Qt::Orientation orientation, QWidget *parent =0)
    : QHeaderView(orientation, parent) {
    // Default options used in QTreeView/QTreeWidget:

    QSize sizeHint() const {
        return QHeaderView::sizeHint().expandedTo(minimumSize());


    // With that in the constructor
    tree->setHeader(new HeaderView(Qt::Horizontal, tree));

    // And to change the size/height
    // The function/slot updateGeometries is protected, so we can't call it
    // directly from outside a QTreeView derived class
    QMetaObject::invokeMethod(tree, "updateGeometries");

  • Thanks Alex, that's perfect :)

Log in to reply