qtablewidget单元格内容设置左对齐
时间: 2024-05-21 12:16:41 浏览: 12
可以使用QTableWidgetItem的setTextAlignment()方法设置单元格内容的对齐方式。示例代码如下:
```python
from PyQt5.QtWidgets import QTableWidget, QTableWidgetItem, QApplication, QHBoxLayout, QWidget
from PyQt5.QtCore import Qt
app = QApplication([])
table_widget = QTableWidget()
table_widget.setRowCount(2)
table_widget.setColumnCount(2)
for row in range(table_widget.rowCount()):
for column in range(table_widget.columnCount()):
item = QTableWidgetItem()
item.setText(f"({row}, {column})")
item.setTextAlignment(Qt.AlignLeft) # 设置对齐方式为左对齐
table_widget.setItem(row, column, item)
layout = QHBoxLayout()
layout.addWidget(table_widget)
widget = QWidget()
widget.setLayout(layout)
widget.show()
app.exec_()
```
运行结果如下图所示:
![qtablewidget-left-align.png](https://i.loli.net/2021/09/16/5CTMeGdR2j1Q8xw.png)
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)