Reputation: 49
Having read some similar posts here, I am still struggling to create a table using PyQt5 and Python 3.8 where I can control the row height.
The above image was captured from the code that follows. The rows are widely spaced, so I can display much less information than analogous tables like tkinter's Treeview. The row size defaults to 25 pixels and I cannot find a way to shrink it.
The usual resizeRowsToContents and setRowHeight don't seem to do anything. How do I fix this?
Here is the routine that adds each row:
def loadTable(self, row, names):
row = self.castTable.rowCount()
riter = iter(names)
self.castTable.insertRow(row)
#self.castTable.verticalHeader().setDefaultSectionSize(8)
self.castTable.setItem(row, 0, QTableWidgetItem(next(riter)))
self.castTable.setItem(row, 1, QTableWidgetItem(next(riter)))
self.castTable.setItem(row, 2, QTableWidgetItem(next(riter)))
#self.castTable.setRowHeight(row, 20)
self.castTable.resizeRowsToContents()
And here is the main code that creates the table:
def build(self):
self.setGeometry(300, 300, 500, 500) #set up window
self.setWindowTitle('Cast list')
grid = QGridLayout() # and layout
# create an empty 3-column table, no grid lines or vertical header
self.castTable = QTableWidget(0, 3, self)
self.castTable.verticalHeader().setVisible(False)
self.castTable.setShowGrid(False)
# table headers
grid.addWidget(self.castTable, 0, 0, 5, 1)
columns = ["Frname", "Lname", "Role"]
self.castTable.setHorizontalHeaderLabels(columns)
# self.castTable.horizontalHeader().font()
# column widths
self.castTable.setColumnWidth(0, 100)
self.castTable.setColumnWidth(1, 150)
self.castTable.setColumnWidth(2, 175)
# add 3 rows
names = ["Edwin", "Rodridguez", "Marco"]
self.loadTable(0, names)
names = ["Mike", "Costantino", "Guiseppe"]
self.loadTable(1, names)
names = ["John", "Matilaine", "Don Alhambra"]
self.loadTable(2, names)
# set the layout and show the table
self.setLayout(grid)
self.show()
Upvotes: 1
Views: 4627
Reputation: 13651
Try it:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *
class MainWindow(QWidget):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setGeometry(700, 200, 500, 500) #set up window
self.setWindowTitle('Cast list')
grid = QGridLayout() # and layout
# create an empty 3-column table, no grid lines or vertical header
self.castTable = QTableWidget(0, 3, self)
# self.castTable.verticalHeader().setVisible(False) #
self.castTable.setShowGrid(False)
# table headers
grid.addWidget(self.castTable, 0, 0, 5, 1)
columns = ["Frname", "Lname", "Role"]
self.castTable.setHorizontalHeaderLabels(columns)
# self.castTable.horizontalHeader().font()
# column widths
self.castTable.setColumnWidth(0, 100)
self.castTable.setColumnWidth(1, 150)
self.castTable.setColumnWidth(2, 175)
# add 3 rows
names = ["Edwin", "Rodridguez", "Marco"]
self.loadTable(0, names)
names = ["Mike", "Costantino", "Guiseppe"]
self.loadTable(1, names)
names = ["John", "Matilaine", "Don Alhambra"]
self.loadTable(2, names)
# set the layout and show the table
self.setLayout(grid)
for row in range(self.castTable.rowCount()): #
print(f'row={row}, rowHeight={self.castTable.rowHeight(row)}') #
def loadTable(self, row, names):
row = self.castTable.rowCount()
riter = iter(names)
self.castTable.insertRow(row)
#self.castTable.verticalHeader().setDefaultSectionSize(8)
self.castTable.setItem(row, 0, QTableWidgetItem(next(riter)))
self.castTable.setItem(row, 1, QTableWidgetItem(next(riter)))
self.castTable.setItem(row, 2, QTableWidgetItem(next(riter)))
print(f'row = {(row+2) * 8}') #
self.castTable.setRowHeight(row, (row+2) * 8) # +++
# setRowHeight
# self.castTable.resizeRowsToContents() # ---
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.setStyle('Fusion') # +++
Form = MainWindow()
Form.show()
sys.exit(app.exec_())
Upvotes: 1