[Interest] QTreeView::resizeColumnToContents not accurate enough

Elvis Stansvik elvstone at gmail.com
Thu Feb 2 11:23:18 CET 2017


WIth the following minimal example:

#include <QApplication>
#include <QTreeView>
#include <QStandardItemModel>
#include <QStandardItem>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QStandardItemModel model(3, 2);
    model.setHorizontalHeaderLabels({ "Header 1", "Header 2" });

    for (int row = 0; row < 3; ++row) {
        for (int column = 0; column < 2; ++column) {
            model.setItem(row, column, new QStandardItem("Foo"));
        }
    }

    QTreeView view;
    view.setModel(&model);
    view.show();
    view.resizeColumnToContents(0);

    return app.exec();
}

I get the attached result. Note how the first column does not snugly
fit the contents. The limiting factor should be the width of the
header ("Header 1"), but there's plenty of room left to shrink the
column.

Anyone else had this problem? Is there a way to improve the accuracy
of QTreeView::resizeColumnToContents ?

This is with Qt 5.5.1 (Ubuntu 16.04 package).

Thanks in advance,
Elvis
-------------- next part --------------
A non-text attachment was scrubbed...
Name: treeviewtest.png
Type: image/png
Size: 6225 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20170202/4e471b5c/attachment.png>


More information about the Interest mailing list