设置QTreeWidget的列宽

时间:2013-02-04 16:56:53

标签: qt pyqt pyside qtreewidget

有没有办法从代码中为QTreeWidget设置列宽?我想chage第一列的默认宽度。我正在使用PySide。

3 个答案:

答案 0 :(得分:7)

QHeaderView::resizeSection()应该诀窍,在C ++中它看起来像这样:

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

答案 1 :(得分:1)

对于正在寻找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 :(得分:0)

在Pyside2中没有resizeSection

您可以在PySide2中使用它:

header = self.treeWidget.header()
header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
header.setStretchLastSection(False)
header.setSectionResizeMode(5, QtWidgets.QHeaderView.Stretch)