qtableview怎么设置前两行重叠显示,或者设置第一行最后四个字右对齐,其他字居中对齐
时间: 2024-01-10 20:05:08 浏览: 31
要设置QTableView的前两行重叠显示,可以使用QHeaderView的setOffset()函数。在重叠的行中,您可以使用QStandardItemModel设置不同的数据以实现所需的效果。
要设置第一行最后四个字右对齐,您可以使用QStyledItemDelegate,重写paint()函数,并在所需的项目中使用此委托。示例代码如下:
```python
class CustomDelegate(QStyledItemDelegate):
def paint(self, painter, option, index):
text = index.data()
if index.row() == 0:
text = text[:-4].rjust(4) + text[-4:]
super().paint(painter, option, index.sibling(index.row(), index.column()), text)
```
对于其他行,您可以使用QStandardItemModel设置居中对齐的文本对齐方式。示例代码如下:
```python
model = QStandardItemModel()
for i in range(rows):
item = QStandardItem("Text")
if i == 0:
item.setTextAlignment(Qt.AlignRight | Qt.AlignVCenter)
else:
item.setTextAlignment(Qt.AlignCenter)
model.setItem(i, j, item)
tableView.setModel(model)
```
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)