为QTreeWidget设置列宽

7
有没有办法从代码中设置QTreeWidget的列宽?我想改变第一列的默认宽度。我正在使用PySide。
3个回答

8

QHeaderView::resizeSection()可以解决这个问题,在C++中应该是这样的:

myTreeWidget->headerView()->resizeSection(0 /*column index*/, 100 /*width*/);

我认为我可以将其用于PySide。谢谢。 - xander27
2
QTreeWidget中没有headerView函数。我认为你真正想使用的是QTreeWidget::header()(至少对于Qt5而言——也许答案是针对Qt4的)。 - Raven

3

对于寻找 C++ Qt 解决方案的人(已在 5.12 版本上测试):

// Important to call setMinimumSectionSize because resizeSection wont work if your width is less than the minimum
treeWidget->header()->setMinimumSectionSize(25);
treeWidget->header()->resizeSection(1 /*column index*/, 25 /*width*/);

// You might also need to use this if you want to limit the size of your last column:
treeWidget->header()->setStretchLastSection(false);

2
在Pyside2中没有resizeSection函数。你可以在PySide2中使用以下方法:
header = self.treeWidget.header()
header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
header.setStretchLastSection(False)
header.setSectionResizeMode(5, QtWidgets.QHeaderView.Stretch)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接