QTableview设置焦点单元格样式为透明,透明度为100% C++
时间: 2024-05-15 13:14:07 浏览: 9
你可以通过设置QTableView的样式表来实现。
例如,如果你的QTableView对象名为tableView,那么你可以在代码中添加以下语句:
```cpp
tableView->setStyleSheet("QTableView:focus { background-color: transparent; opacity: 1.0; }");
```
这将使得QTableView中被选中的单元格背景色为透明,并将透明度设置为100%。你可以根据自己的需求调整样式表中的属性值。
相关问题
QT Qtableview 焦点设置为选中当前单元格
可以通过设置QTableView的selectionBehavior属性为QAbstractItemView::SelectItems来实现选中当前单元格。具体代码如下:
```cpp
tableView->setSelectionBehavior(QAbstractItemView::SelectItems);
tableView->setCurrentIndex(tableView->currentIndex());
```
其中,setCurrentIndex函数将当前选中的单元格设置为当前焦点。注意,如果要保持选中状态,需要将QTableView的selectionMode属性设置为QAbstractItemView::SingleSelection,表示只能选择一个单元格。
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` 类中,我们实现了一个简单的表格模型,用于在表格中显示数据。最后,我们将自定义的代理设置给表格视图,这样就可以在表格中显示自定义的单元格样式了。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)