How can we set the width of a vertical header in a TableView when new rows are added.?

  • I tried using the QHeaderView, kept the orientation as Vertical. Then with the help of the sizeHint of the QHeaderView class like below:

    @QSize QHeaderView::sizeHint() const
    Q_D(const QHeaderView);
    if (d->cachedSizeHint.isValid())
    return d->cachedSizeHint;
    d->cachedSizeHint = QSize(0, 0); //reinitialize the cached size hint
    const int sectionCount = count();

    // get size hint for the first n sections
    int i = 0;
    for (int checked = 0; checked < 100 && i < sectionCount; ++i) {
        if (isSectionHidden(i))
        QSize hint = sectionSizeFromContents(i);
        d->cachedSizeHint = d->cachedSizeHint.expandedTo(hint);
    // get size hint for the last n sections
    i = qMax(i, sectionCount - 100 );
    for (int j = sectionCount - 1, checked = 0; j >= i && checked < 100; --j) {
        if (isSectionHidden(j))
        QSize hint = sectionSizeFromContents(j);
        d->cachedSizeHint = d->cachedSizeHint.expandedTo(hint);
    return d->cachedSizeHint;


    Then I used the geometriesChanged function of the QTableView. which is:
    void QTableView::updateGeometries()
    if (d->geometryRecursionBlock)
    d->geometryRecursionBlock = true;

    int width = 0;
    if (!d->verticalHeader->isHidden()) {
        width = qMax(d->verticalHeader->minimumWidth(), d->verticalHeader->sizeHint().width());
        width = qMin(width, d->verticalHeader->maximumWidth());
    int height = 0;
    if (!d->horizontalHeader->isHidden()) {
        height = qMax(d->horizontalHeader->minimumHeight(), d->horizontalHeader->sizeHint().height());
        height = qMin(height, d->horizontalHeader->maximumHeight());
    bool reverse = isRightToLeft();
     if (reverse)
         setViewportMargins(0, height, width, 0);
         setViewportMargins(width, height, 0, 0);
    // update headers
    QRect vg = d->viewport->geometry();
    int verticalLeft = reverse ? vg.right() + 1 : (vg.left() - width);
    d->verticalHeader->setGeometry(verticalLeft,, width, vg.height());
    if (d->verticalHeader->isHidden())
        QMetaObject::invokeMethod(d->verticalHeader, "updateGeometries");
    int horizontalTop = - height;
    d->horizontalHeader->setGeometry(vg.left(), horizontalTop, vg.width(), height);
    if (d->horizontalHeader->isHidden())
        QMetaObject::invokeMethod(d->horizontalHeader, "updateGeometries");


    I called this function in a signal slot connection when new columns are added. But still the size is the minimumwidth(20) or something which I set in the starting.. Even though there are index in the vertival footer with numbers upto 5000 and these are displayed so small because of 20 minimumwidth I set.

    If what I think is wrong, please sugggest another idea. I just want it to change and get set dynamically.

    Thanks in advance.

  • Lifetime Qt Champion


    Something is not clear, QHeaderView should resize itself automatically. What problem did you encounter ?

  • Hii SGaist

    The width of the vertical header is not set automatically. I dont know why.. Can you tell me why? The minimum width that I set in the constructor is the fixed width but changing even though there are more numbers of more width added.

  • Lifetime Qt Champion

    Can you show a minimal sample for code that shows that the header is not resized ?

  • Hey SGaist, I found the way for setting it. I solved it. I used a signal from the QHeaderView : sectionCountChanged(int, int).. I connected it to a public slot in my class that inherits QTableView.
    In the connected slot, I took the font of the tableview and then considered the width of the last number of the index in the vertical header. There could be a small offset but I added a offset of 4 pixels and set this as the minimumwidth of the vertical header. The width gets updated when new rows are added.

    my header file containing the slot is:
    public slots:
    void slotVerticalSectionsCountChanged(int xOld, int xNew);

    My cpp file contains the connect in the constructor like:
    connect(verticalHeader(), SIGNAL(sectionCountChanged(int, int)), this, SLOT(slotVerticalSectionsCountChanged(int, int)));

    The slot is defined as follows:
    void xxxxxxxx::slotVerticalSectionsCountChanged(int xOld, int xNew)

    // Get the current no. of rows of the table view
    QString tString = count(); 

    // QString tString = xNew; // xNew is the current rows

    // The font of the current table or the vertical header is taken for calculating the width of the index value of the last index
    QFont font = verticalHeader()->font();
    QFontMetrics tFm(font);
    // 4 pixels are added as buffer pixels since the margins slightly affect the view of the vertical header
    int width = tFm.width(tString) + 4;
    // We set this value as the width


    Hope this could be a help for anyone in the future. Cheers!

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.