14

I am a student programmer and I am using Qt to build a GUI interface for work. I have a QTreeWidget that has a total of 8 Columns. The input that needs to be entered into each one of these columns is fairly small. In that regard, I was trying to find a way to re-size all of the columns to the least amount of space required. much like the action that is performed when you click on the dividers. I have looked over several websites and the Qt documentation and haven't found a solution. After looking over the documentation it seems that there are a bunch of ways to do this but I cant get one to work. I am beginning to think that I need to enable/disable something somewhere. It's as if the changes I write in to my build function (This function clears the table; Reads data from my vector, displays certain data in the table, and then is suppose to re-size the headers/columns to just enough size to fit the contents of the column) are not even applied. I have tried the following with no changes:

        ui->treeWidgetInjections->setColumnWidth(i, minimumWidth());
        ui->treeWidgetInjections->resizeColumnToContents(i);
        ui->treeWidgetInjections->header()->ResizeToContents;
        ui->treeWidgetInjections->header()->resizeSection(i, minimumWidth());

All of these are in the end of of my build Function (unrelated info removed):

ui->treeWidgetInjections->clear();
    for (int i=0; i < qTreeInjectionData.size(); i++)
        {
            //fill Qtree with items;
            //fill QTree items with data;
            //re size function here;
        }

Please only leave productive feedback as I am only interested in learning and overcoming this challenge. Thanks in advance!

Freedom_Ben
  • 11,247
  • 10
  • 69
  • 89
Wylie Coyote SG.
  • 1,009
  • 6
  • 22
  • 37

2 Answers2

29

This minimal example works for me:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QVBoxLayout *layout = new QVBoxLayout(centralWidget());
    QTreeWidget *tree = new QTreeWidget(this);
    layout->addWidget(tree);

    tree->setColumnCount(3);

    QStringList first, second, third;
    first  << "xxxxxxxxx"      << "xxxxxxxxxxxxx"     << "xxxxxxxx";
    second << "xxxxx"          << "xxxxxxxxxxxxxxxxx" << "xxxxx";
    third  << "xxxxxxxxxxxxxx" << "xxxxxxxxxx"        << "xxxxxxxx";

    tree->insertTopLevelItem(0, new QTreeWidgetItem(first));
    tree->insertTopLevelItem(0, new QTreeWidgetItem(second));
    tree->insertTopLevelItem(0, new QTreeWidgetItem(third));

    for(int i = 0; i < 3; i++)
        tree->resizeColumnToContents(i);
}

All you need to do is call the resizeColumnToContents() method once for every column after you populate the view. Let me know if the problem persists.

Pietro Lorefice
  • 1,477
  • 15
  • 19
  • Its really strange but when I created a separate for loop in my build function for my column re-sizes it worked fine... very strange. THANKS FOR YOUR HELP! Your example helped me in a big way and I am sure this will help others! – Wylie Coyote SG. Jan 25 '12 at 20:41
  • 6
    Unfortunately, This method only resize according to the contents of the header for me. Data displayed word-wrapped. – Hareen Laks Sep 15 '15 at 06:27
  • make sure it's `resizeColumnToContents` not `resizeColumn[S]ToContents` – azzamsa Feb 19 '19 at 12:40
  • @HareenLaks same here, the answers here have not helped me. Have you found a way? – László Papp May 11 '22 at 05:45
18

The problem for me was that StretchLastSection was set to true and if this value is set to true, this property will override the resize mode set on the last section in the header.

Solution:

_treeWidget = new UIWidgetSceneTree();
_treeWidget->header()->setStretchLastSection(false);
_treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
Torkil Kollsker
  • 291
  • 2
  • 4
  • I prefer this way to resize, instead of defining column number one by one. – azzamsa Mar 02 '19 at 03:51
  • 2
    What is a `UIWidgetSceneTree`? Is it your custom subclass of `QTreeView`? It makes this answer a bit confusing. – Arthur Tacca Jan 22 '20 at 19:35
  • I do not know why, but view->resizeColumnToContents(0); did not help on my QTreeView, whilst view->header()->setSectionResizeMode(QHeaderView::ResizeToContents); The problem with this solution though is that the user can no longer resize the header. I feel like that is very limiting. – László Papp May 11 '22 at 05:39