How to align the text to center of cells in a QTableWidget

后端 未结 2 1411
慢半拍i
慢半拍i 2020-12-04 02:22

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

2条回答
  •  不知归路
    2020-12-04 02:37

    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
    

提交回复
热议问题