0

I have a QT Table Widget in Python where I have N columns, for each columns I set a width, but this is not permorming since I could have some text that is bigger than the width I gave them, how can I say for each column a minimun Widht (for example if the cell is empty I don't want the column to disappear, I want to show the title) and also to resize the columns width to text? this is my code:

class elencoviaggi(QtWidgets.QDialog):
  def __init__(self):
    super(elencoviaggi, self).__init__()
    uic.loadUi('elenco_reg.ui', self)
    self.tabella_registrazioni.setColumnWidth(0, 30)
    self.tabella_registrazioni.setColumnWidth(1, 100)
    self.tabella_registrazioni.setColumnWidth(2, 80)
    self.tabella_registrazioni.setColumnWidth(3, 105)
    for row in c.execute("SELECT * FROM Viaggi "):
        righe = self.tabella_registrazioni.rowCount()
        self.tabella_registrazioni.setRowCount(righe+1)
        self.tabella_registrazioni.setItem(tablerow, 0, QtWidgets.QTableWidgetItem(str(row[0])))
        self.tabella_registrazioni.setItem(tablerow, 1, QtWidgets.QTableWidgetItem(row[1]))
        self.tabella_registrazioni.setItem(tablerow, 2, QtWidgets.QTableWidgetItem(row[2]))
Edmond
  • 1
  • 3
  • `self.tabella_registrazioni.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)` – musicamante Jun 04 '22 at 23:54
  • Thanks, it worked, btw I think the question should remain because it's not a duplicate, the other question was in C++ – Edmond Jun 05 '22 at 00:26
  • It doesn't change that much, since the syntax is almost the same (with the small differences between C++ and Python for which `->` and `::` are the same as the dot), and it also already contains a PyQt answer. – musicamante Jun 05 '22 at 00:35

0 Answers0