Hi,
I have a QtWidgets.QTableWidget
with 7 columns. I am settling for a left justified all columns with setDefaultAlignment(QtCore.Qt.AlignmentFlag.AlignLeft)
:
self.employeeList = QtWidgets.QTableWidget(parent=self.containerFrame)
self.employeeList.setColumnWidth(0, 100) # Emp. No.
self.employeeList.setColumnWidth(1, 250) # Email
self.employeeList.setColumnWidth(2, 150) # Birth Date
self.employeeList.setColumnWidth(3, 150) # First Name
self.employeeList.setColumnWidth(4, 150) # Last Name
self.employeeList.setColumnWidth(5, 80) # Gender
self.employeeList.setColumnWidth(6, 150) # Hire Date
self.employeeList.setHorizontalHeaderLabels(
['Emp. No.', 'Email', 'Birth Date', 'First Name',
'Last Name', 'Gender', 'Hire Date'])
header = self.employeeList.horizontalHeader()
header.setSectionResizeMode(0, QHeaderView.ResizeMode.Fixed) # Emp. No.
header.setSectionResizeMode(1, QHeaderView.ResizeMode.Stretch) # Email
header.setSectionResizeMode(2, QHeaderView.ResizeMode.Fixed) # Birth Date
header.setSectionResizeMode(3, QHeaderView.ResizeMode.Fixed) # First Name
header.setSectionResizeMode(4, QHeaderView.ResizeMode.Fixed) # Last Name
header.setSectionResizeMode(5, QHeaderView.ResizeMode.Fixed) # Gender
header.setSectionResizeMode(6, QHeaderView.ResizeMode.Fixed) # Hire Date
# ALL COLUMNS ARE LEFT JUSTIFIED.
header.setDefaultAlignment(QtCore.Qt.AlignmentFlag.AlignLeft)
I would like to set different alignments for column header. Is it possible please?
If it is possible, would you be able to show an example code, please.
Please note: I understand how to set alignment for data row. For example, to centre-aligned Emp. No.
data column:
self.employeeList.item(row, 0).setTextAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
– But I don’t know how to do it for the header.*
Thank you and best regards,
…behai.