Qtableview设置全部单元格和边框之间没有空隙
时间: 2024-05-10 07:18:37 浏览: 14
可以通过设置QTableView的边框线宽度为0来实现。具体代码如下:
```python
tableView.setStyleSheet("QTableView {border: none;}") # 去掉边框线
tableView.verticalHeader().setSectionResizeMode(QHeaderView.Fixed) # 固定行高
tableView.verticalHeader().setDefaultSectionSize(20) # 设置行高
tableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) # 列宽自适应
tableView.verticalScrollBar().setStyleSheet("QScrollBar:vertical {width: 0px;}") # 隐藏垂直滚动条
tableView.setShowGrid(False) # 去掉网格线
```
这样就可以实现QTableView的全部单元格和边框之间没有空隙的效果。
相关问题
Qtableview设置全部单元格自适应tableview大小
可以通过以下步骤设置QTableView中所有单元格自适应QTableView的大小:
1. 设置QTableView的大小策略,使其随着父窗口的大小变化而自适应调整大小:
```python
tableView.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents)
```
2. 设置QTableView的伸缩因子,使其在父窗口大小变化时保持比例调整大小:
```python
tableView.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
tableView.verticalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
```
3. 在QTableView的模型中添加数据,并根据需要设置行和列的大小:
```python
model = QtGui.QStandardItemModel()
tableView.setModel(model)
# 添加数据到模型中
# 设置行和列的大小
tableView.resizeColumnsToContents()
tableView.resizeRowsToContents()
```
这些步骤将使QTableView中的所有单元格自适应QTableView的大小。
Qtableview 设置单元格 item样式
如果想在 Qt 的 QTableView 中设置单元格的样,可以通过自定义 QStyledItemDelegate 类来实现。以下是一个简单的示例:
```python
from PyQt5.QtWidgets import QStyledItemDelegate, QStyleOptionViewItem, QApplication, QTableView, QWidget
from PyQt5.QtCore import Qt, QAbstractTableModel, QModelIndex
class CustomDelegate(QStyledItemDelegate):
def paint(self, painter, option, index):
# 自定义绘制单元格的样式
painter.save()
if option.state & QStyle.State_Selected:
painter.fillRect(option.rect, option.palette.highlight())
painter.drawText(option.rect, Qt.AlignCenter, str(index.data()))
painter.restore()
class MyTableModel(QAbstractTableModel):
def __init__(self, data):
super().__init__()
self._data = data
def rowCount(self, parent):
return len(self._data)
def columnCount(self, parent):
return len(self._data[0])
def data(self, index, role):
if role == Qt.DisplayRole:
return self._data[index.row()][index.column()]
return None
if __name__ == '__main__':
app = QApplication([])
table = QTableView()
data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
model = MyTableModel(data)
delegate = CustomDelegate()
table.setModel(model)
table.setItemDelegate(delegate)
table.show()
app.exec_()
```
在这个示例中,我们定义了一个名为 `CustomDelegate` 的 QStyledItemDelegate 子类,重写了 `paint` 方法,实现了自定义绘制单元格的样式。在 `MyTableModel` 类中,我们实现了一个简单的表格模型,用于在表格中显示数据。最后,我们将自定义的代理设置给表格视图,这样就可以在表格中显示自定义的单元格样式了。
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.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)