qtablewidget单元格内容设置左对齐
时间: 2024-05-21 15:16:41 浏览: 81
可以使用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)
阅读全文