qt create tableview设置隐藏指定一行
时间: 2023-09-06 21:12:17 浏览: 146
如果您想在 Qt 的 TableView 中隐藏特定行,可以使用 `QAbstractItemModel` 中的 `flags()` 方法来设置特定行的标志为不可编辑和不可选择。具体步骤如下:
1. 自定义一个继承自 `QStandardItemModel` 的模型类,并重写 `flags()` 方法。在这个方法中,通过判断当前行数是否为需要隐藏的行数,然后将该行的标志设置为不可编辑和不可选择。
```cpp
class MyModel : public QStandardItemModel
{
public:
Qt::ItemFlags flags(const QModelIndex &index) const override
{
Qt::ItemFlags flags = QStandardItemModel::flags(index);
if (index.row() == 2) // 隐藏第 3 行
flags &= ~(Qt::ItemIsEditable | Qt::ItemIsSelectable);
return flags;
}
};
```
2. 在 `MainWindow` 中创建一个 `QTableView` 对象,并将上面自定义的模型设置给它。
```cpp
MyModel *model = new MyModel(this);
model->setColumnCount(3);
model->setRowCount(5);
model->setHeaderData(0, Qt::Horizontal, "Name");
model->setHeaderData(1, Qt::Horizontal, "Age");
model->setHeaderData(2, Qt::Horizontal, "Gender");
ui->tableView->setModel(model);
```
这样,第 3 行就会被隐藏,无法编辑和选择。
相关推荐
![](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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)