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

前端 未结 5 1175
无人及你
无人及你 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:35

    You can do this with QItemDelegates or QStyledItemDelegates. If you want to resize to contents and have automatic stretch, you'll need to choose which column is the "stretch" column.

    class ResizeDelegate(QStyledItemDelegate):
    
        def __init__(self, table, stretch_column, *args, **kwargs):
            super(ResizeDelegate, self).__init__(*args, **kwargs)        
            self.table = table
            self.stretch_column = stretch_column
    
        def sizeHint(self, option, index):
            size = super(ResizeDelegate, self).sizeHint(option, index)
            if index.column() == self.stretch_column:
                total_width = self.table.viewport().size().width()
                calc_width = size.width()
                for i in range(self.table.columnCount()):
                    if i != index.column():
                        option_ = QtGui.QStyleOptionViewItem()
                        index_ = self.table.model().index(index.row(), i)
                        self.initStyleOption(option_, index_)
                        size_ = self.sizeHint(option_, index_)
                        calc_width += size_.width()
                if calc_width < total_width:
                    size.setWidth(size.width() + total_width - calc_width)
            return size
    
    ...
    
    table = QTableWidget()
    delegate = ResizeDelegate(table, 0)
    table.setItemDelegate(delegate)
    ... # Add items to table
    table.resizeColumnsToContents()
    

    You can set the resize mode to ResizeToContents, or if you want the user to be able to adjust the column width as needed, just call resizeColumnsToContents manually after making changes to the table items.

    You also may need to fudge around with the width calculations a bit because of margins and padding between columns (like add a pixel or two to the calculated_width for each column to account for the cell border).

    0 讨论(0)
  • 2020-12-23 22:46

    I have used this and setMinumumsectionsize has solved my problem of Qtableview cell size

    self.questionWidget.tableView.setModel(self.model)
    self.questionWidget.tableView.verticalHeader().setVisible(False)
    self.questionWidget.tableView.horizontalHeader().setMinimumSectionSize(200)
    
    
    0 讨论(0)
  • 2020-12-23 22:48

    PyQt4

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

    PyQt5

    header = self.table.horizontalHeader()       
    header.setSectionResizeMode(0, QtWidgets.QHeaderView.Stretch)
    header.setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)
    header.setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)
    header.setSectionResizeMode(3, QtWidgets.QHeaderView.Stretch)
    
    0 讨论(0)
  • 2020-12-23 22:53

    This can be solved by setting the resize-mode for each column. The first section must stretch to take up the available space, whilst the last two sections just resize to their contents:

    PyQt4:

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

    PyQt5:

    header = self.table.horizontalHeader()       
    header.setSectionResizeMode(0, QtWidgets.QHeaderView.Stretch)
    header.setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)
    header.setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题