How to align the text to center of cells in a QTableWidget(如何将文本与 QTableWidget 中的单元格中心对齐)
问题描述
我使用的是基于 Qt4 的 PyQt.我的编辑器是 PyCharm 2017.3,我的 Python 版本是 3.4.我正在从网站上抓取一些文本.我正在尝试将该文本与 QTableWidget 中单元格的中心对齐.
item = QTableWidgetItem(scraped_age).setTextAlignment(Qt.AlignHCenter)self.tableWidget.setItem(x, 2,item)因此,在将项目放入单元格时,我试图按照
当我删除 setTextAlignment 方法时确实出现了,如下所示
item = QTableWidgetItem(scraped_age)self.tableWidget.setItem(x, 2,item)这行代码:
item = QTableWidgetItem(scraped_age).setTextAlignment(Qt.AlignHCenter) 将无法正常工作,因为它会在将其分配给变量之前丢弃它创建的项目.该变量实际上将设置为None,这是setTextAlignment() 的返回值.相反,您必须这样做:
item = QTableWidgetItem(scraped_age) # 创建项目item.setTextAlignment(Qt.AlignHCenter) # 改变对齐方式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.
It did show up when I removed setTextAlignment method as shown below
item = QTableWidgetItem(scraped_age)
self.tableWidget.setItem(x, 2,item)
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
这篇关于如何将文本与 QTableWidget 中的单元格中心对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将文本与 QTableWidget 中的单元格中心对齐
基础教程推荐
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 包装空间模型 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
