How to align the text to center of cells in a QTableWidget
Asked Answered
P

4

15

I am using PyQt based on Qt4. My Editor is PyCharm 2017.3 and my python version is 3.4. I am scraping some text from a website. I am trying to align that text to the center of the cell in a QTableWidget.

item = QTableWidgetItem(scraped_age).setTextAlignment(Qt.AlignHCenter)
self.tableWidget.setItem(x, 2,item)

Therefore while putting the item in the cell, I am trying to align it as per the documentation. The problem is that the data is not showing up.

a part of QtableWidget where data is not showing up

It did show up when I removed setTextAlignment method as shown below

item = QTableWidgetItem(scraped_age)
self.tableWidget.setItem(x, 2,item)

a part of my QtableWidget

Philadelphia answered 25/12, 2017 at 19:0 Comment(0)
L
27

This line of code:

item = QTableWidgetItem(scraped_age).setTextAlignment(Qt.AlignHCenter)

will not work properly, because it throws away the item it creates before assigning it to the variable. The variable will in fact be set to None, which is the return value of setTextAlignment(). Instead, you must do this:

item = QTableWidgetItem(scraped_age) # create the item
item.setTextAlignment(Qt.AlignHCenter) # change the alignment
Libelee answered 25/12, 2017 at 19:18 Comment(1)
Qt.AlignHCenter will center horizontally at the top of the cell, Qt.AlignCenter will center to the center of the cell (horizontally and vertically).Motorway
T
12

This didn't work for me, and I'm not sure if it is because I'm using PyQt5 or it i did something wrong. I was trying to find something similar but for the whole table, and i finally stumbled upon something that worked and lets you center every cells or just one column at a time.

You have to use the delegate method:

#You're probably importing QtWidgets to work with the table
#but you'll also need QtCore for the delegate class
from PyQt5 import QtCore, QtWidgets

class AlignDelegate(QtWidgets.QStyledItemDelegate):
    def initStyleOption(self, option, index):
        super(AlignDelegate, self).initStyleOption(option, index)
        option.displayAlignment = QtCore.Qt.AlignCenter

After implementing this in your code, you can add the following to your main window class or wherever the table is defined:

delegate = AlignDelegate(self.tableWidget)
self.tableWidget.setItemDelegateForColumn(2, delegate) #You can repeat this line or
                                                       #use a simple iteration / loop
                                                       #to align multiple columns

 #If you want to do it for all columns:
 #self.tableWidget.setItemDelegate(delegate)

Know this is an old question, but hope it can help someone else.

Thievish answered 11/5, 2020 at 4:29 Comment(1)
When using AbstractTable model this delegate mechanism is a way better solution.Ochrea
O
1

Bit late to the party but for those of you wondering how to do this on pyqt5

table = QTableWidgetItem() #QTWidgets.QTableWidgetItem() if importing QWidget from PyQt5

table.setTextAlignment(number)

setTextAlignment takes an int for the argument (alignment). Put the number in to get the result:

0:left 1:left 2:right 3:right 4:centre

Octaviaoctavian answered 7/1, 2022 at 10:11 Comment(0)
D
0

If not using delegates and one doesn't want to set alignment for every QTableWidgetItem created anew, then you could set the prototype item in the table widget like so

protoItem = QTableWidgetItem()
protoItem.setTextAlignment(Qt.AlignCenter)
# set other things like read-only with QTableWidgetItem.setFlags, etc.

# set prototype item for all items created from now
myTableWidget.setItemPrototype(protoItem)
Doormat answered 15/7, 2023 at 6:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.