qtableview 取某行某列值
时间: 2023-12-12 15:34:25 浏览: 34
根据提供的引用内容,可以使用以下代码来获取QTableView中某行某列的值:
```cpp
QModelIndex index = ui->tableView->model()->index(row, column);
QString value = ui->tableView->model()->data(index).toString();
```
其中,row和column分为需要获取值的行和列的索引。需要注意的是,这里的model()函数返回的是QAbstractItemModel类型的指针,需要根据实际情况进行强制类型转换。
相关问题
QTableView点击某行后会滚动
这可能是由于QTableView的默认行为导致的。当用户单击某行时,QTableView会尝试将该行滚动到视图的中心位置,以便用户可以更好地查看该行的内容。
如果您不希望发生这种行为,您可以尝试通过以下方法禁用它:
```python
tableView.verticalScrollMode(QAbstractItemView.ScrollPerPixel)
```
这将使QTableView在滚动时以像素为单位进行滚动,而不是滚动到行的中心位置。
qtableview设置某列输入正则表达式
可以使用 QItemDelegate 的 setValidator() 方法来设置某列输入的正则表达式,具体代码如下:
```cpp
QRegExp regExp("[A-Za-z0-9]+"); // 正则表达式,只允许输入字母和数字
QStandardItemModel* model = new QStandardItemModel();
QTableView* tableView = new QTableView();
tableView->setModel(model);
tableView->setItemDelegateForColumn(1, new QRegExpValidator(regExp, this)); // 设置第1列的输入正则表达式
```
这段代码可以实现在 QTableView 的第1列中只允许输入字母和数字。
相关推荐
![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)