Is there any way to set column width for QTreeWidget from code? I want to chage default width of first column. I'm using PySide.
QHeaderView :: resizeSection()可以做到这一点,在C ++中它看起来像这样:
myTreeWidget->headerView()->resizeSection(0 /*column index*/, 100 /*width*/);
For people looking for a C++ Qt solution (tested with 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
you can use this in PySide2:
header = self.treeWidget.header()
header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
header.setStretchLastSection(False)
header.setSectionResizeMode(5, QtWidgets.QHeaderView.Stretch)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.