Qtableview设置某一列不可点击
时间: 2023-08-18 22:09:57 浏览: 80
可以通过设置QTableView的itemDelegate来实现某一列不可点击。具体步骤如下:
1. 自定义一个QStyledItemDelegate派生类,重写createEditor()和editorEvent()方法,使得在某一列中的单元格无法编辑和点击。
```python
class NoEditDelegate(QStyledItemDelegate):
def createEditor(self, parent, option, index):
return None
def editorEvent(self, event, model, option, index):
if index.column() == 1: # 第二列不可编辑
return False
else:
return super().editorEvent(event, model, option, index)
```
2. 在QTableView中设置这个Delegate。
```python
tableView = QTableView()
delegate = NoEditDelegate()
tableView.setItemDelegateForColumn(1, delegate) # 第二列不可编辑
```
通过这样的方法,就可以实现QTableView中某一列不可点击的效果。
相关问题
qtableview设置某一列不可编辑
在使用QTableView时,可以通过设置model,在Model中将某一列设置为只读,从而实现该列不可编辑的效果。
可以通过以下代码来实现该功能:
```python
# 获取要设置为只读的列号
readonly_column = 1
# 设置model中的某一列为只读
class CustomModel(QStandardItemModel):
def flags(self, index: QModelIndex) -> Qt.ItemFlags:
if index.column() == readonly_column:
return super().flags(index) & ~Qt.ItemIsEditable
return super().flags(index)
# 在创建model时使用自定义model
model = CustomModel()
# 将model设置给TableView
table_view.setModel(model)
```
上述代码中,我们继承了QStandardItemModel,重写了flags函数,并在该函数中使用了位运算来设置只读标记。这样,在数据绑定到TableView中时,该列就会显示为只读。
总之,通过在Model中重写flags函数,并针对需要只读的列进行特殊处理,可以实现TableView中某一列不可编辑的效果。
Qtableview设置某一列不可选中
你可以通过实现 `QItemSelectionModel` 的 `QItemSelectionFlags flags(const QModelIndex &index) const` 函数来禁用某一列的选中状态。步骤如下:
1. 获取 `QTableView` 的 `QItemSelectionModel` 对象:
```cpp
QItemSelectionModel *selectionModel = tableView->selectionModel();
```
2. 自定义 `QItemSelectionModel` 子类并重新实现 `flags()` 函数:
```cpp
class CustomSelectionModel : public QItemSelectionModel
{
public:
CustomSelectionModel(QAbstractItemModel *model)
: QItemSelectionModel(model)
{
}
QItemSelectionFlags flags(const QModelIndex &index) const override
{
QItemSelectionFlags flags = QItemSelectionModel::flags(index);
if (index.column() == 1) {
flags &= ~Qt::ItemIsSelectable;
}
return flags;
}
};
```
3. 将自定义的 `QItemSelectionModel` 对象设置为 `QTableView` 的选中模型:
```cpp
CustomSelectionModel *customSelectionModel = new CustomSelectionModel(tableView->model());
tableView->setSelectionModel(customSelectionModel);
```
这样就可以禁用第二列的选中状态了。
相关推荐
![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)