Set column width for QTreeWidget

一世执手 提交于 2019-12-22 05:04:52

问题


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.


回答1:


QHeaderView::resizeSection() should do the trick, in C++ it would look like this:

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



回答2:


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);



回答3:


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)


来源:https://stackoverflow.com/questions/14691525/set-column-width-for-qtreewidget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!