code: def resizeColumnsToContents(self, startCol=None, stopCol=None):
          
          numCols = self.model().columnCount()
          startCol = 0 if startCol is None else max(startCol, 0)
          stopCol  = numCols if stopCol is None else min(stopCol, numCols)
  
          row = 0
          for col in range(startCol, stopCol):
              indexWidget = self.indexWidget(self.model().index(row, col))
  
              if indexWidget:
                  contentsWidth = indexWidget.sizeHint().width()
              else:
                  contentsWidth = self.header().sectionSizeHint(col)
  
              self.header().resizeSection(col, contentsWidth)
docs: Resizes all columns to the contents