Is there way to automatically resize content of the last column for QTableView?

210 views Asked by At

I have table view with 6 columns in my app. All columns are interactive and a last one also stretches via function QTableView::setStretchLastSection. However, if I try to resize main window from maximum size to minimum(1170x686 for my app), the last column stays stretched. I made a workaround:

void MyTableView::resizeEvent(QResizeEvent* event)
{
    QWidget* widget = parentWidget();
    while (widget != nullptr && widget->parentWidget() != nullptr)
    {
        widget = widget->parentWidget();
    }
    if (widget != nullptr)
    {
        const int width = widget->width();
        const int height = widget->height();
        static const QSize s_minimumWindowSize(1170, 686);
        QAbstractItemModel* currentModel = model();
        if (width <= s_minimumWindowSize.width() && height <= s_minimumWindowSize.height() && currentModel != nullptr)
        {
            setColumnWidth(currentModel->columnCount() - 1, GetMinimumWidthForColumn());
        }
    }
    QTableView::resizeEvent(event);
}

Is there a better way to fix this issue?

1

There are 1 answers

0
Sphics On

Check the size policy of your table. Setting both the horizontal and vertical policies to ignored usually fixes this problem for me.