PySide - PyQt : How to make set QTableWidget column width as proportion of the available space?

前端 未结 5 1189
无人及你
无人及你 2020-12-23 22:19

I\'m developing a computer application with PySide and I\'m using the QTableWidget. Let\'s say my table has 3 columns, but the data they contain is very different,

5条回答
  •  甜味超标
    2020-12-23 22:57

    As mentioned before, you can do this by setting the resize-mode of each column. However, if you have a lot of columns this can be a lot of code. The way I do it is setting the "general" resize-mode to "ResizeToContent" and than for one (or more) columns to "Stretch"!

    Here is the code:

    PyQt4:

    header = self.table.horizontalHeader()
    header.setResizeMode(QtGui.QHeaderView.ResizeToContents)
    header.setResizeMode(0, QtGui.QHeaderView.Stretch)
    

    PyQt5:

    header = self.table.horizontalHeader()
    header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)       
    header.setSectionResizeMode(0, QtWidgets.QHeaderView.Stretch)
    

提交回复
热议问题