简体   繁体   English

设置QTreeWidget的列宽

[英]Set column width for QTreeWidget

Is there any way to set column width for QTreeWidget from code? 有没有办法从代码中为QTreeWidget设置列宽? I want to chage default width of first column. 我想chage第一列的默认宽度。 I'm using PySide. 我正在使用PySide。

QHeaderView :: resizeSection()可以做到这一点,在C ++中它看起来像这样:

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

For people looking for a C++ Qt solution (tested with 5.12): 对于寻找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);

In Pyside2 there is no resizeSection 在Pyside2中没有resizeSection

you can use this in PySide2: 你可以在PySide2中使用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM