QtWidgets.QTableWidget How To Set Individual Column Header Alignment, Please Help

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.

Hi @behai-nguyen,
Here is small working example:

from PySide6.QtWidgets import QApplication, QTableWidget, QTableWidgetItem, QHeaderView
from PySide6.QtCore import Qt

app = QApplication([])

table = QTableWidget(5, 3)
table.setHorizontalHeaderLabels(["Column 1", "Column 2", "Column 3"])

header = table.horizontalHeader()
header.setDefaultAlignment(Qt.AlignmentFlag.AlignCenter)

for row in range(5):
    for column in range(3):
        item = QTableWidgetItem(f"Cell {row + 1},{column + 1}")
        item.setTextAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
        table.setItem(row, column, item)

table.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Stretch)
table.show()

app.exec()

Hi @Michal_Plichta,

Thank you kindly for your helps, I appreciate it.

This what we have when I run the example:

This is, I think, the default behaviour: the header row is centre-aligned, in my example, I set it the left.

What I am trying to achieve, is to set each header column alignment differently. For example, the header Column 1 in the above screenshot to be left-aligned, while leaving the rest as they are.

Thank you and best regards,

…behai.

Ok, it took ma a while to find out! So you need use, setHeaderData() on each cell form header.

BTW, I learn more and more with you questions… like it a lot!

from PySide6.QtWidgets import QApplication, QTableWidget, QTableWidgetItem, QHeaderView
from PySide6.QtCore import Qt

app = QApplication([])

table = QTableWidget(5, 3)
table.setHorizontalHeaderLabels(["Column 1", "Column 2", "Column 3"])

header_model = table.model()

header_model.setHeaderData(0, Qt.Orientation.Horizontal, Qt.AlignmentFlag.AlignLeft, Qt.ItemDataRole.TextAlignmentRole)
header_model.setHeaderData(1, Qt.Orientation.Horizontal, Qt.AlignmentFlag.AlignCenter, Qt.ItemDataRole.TextAlignmentRole)
header_model.setHeaderData(2, Qt.Orientation.Horizontal, Qt.AlignmentFlag.AlignRight, Qt.ItemDataRole.TextAlignmentRole)

for row in range(5):
    for column in range(3):
        item = QTableWidgetItem(f"Cell {row + 1},{column + 1}")
        item.setTextAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
        table.setItem(row, column, item)

table.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Stretch)
table.show()

app.exec()