c++ QT5.7将QTableview左边第一列自动生成的索引列关闭
时间: 2024-01-06 20:03:16 浏览: 47
您可以使用QTableView的setVerticalHeaderVisible方法将左边的垂直表头隐藏,从而关闭自动生成的索引列。示例代码如下:
```cpp
tableView->setVerticalHeaderVisible(false);
```
这样就可以关闭QTableView左边第一列自动生成的索引列了。需要注意的是,在QT5.7中,QTableView的setVerticalHeaderVisible方法已经被弃用,建议使用setVerticalHeaderHidden方法。
```cpp
tableView->setVerticalHeaderHidden(true);
```
同样可以实现关闭自动生成的索引列。
相关问题
将QTableview左边第一列自动生成的索引列关闭
您可以使用QTableView的setVerticalHeaderVisible方法将左边的垂直表头隐藏,从而关闭自动生成的索引列。示例代码如下:
``` python
tableView.setVerticalHeaderVisible(False)
```
这样就可以关闭QTableView左边第一列自动生成的索引列了。
QT C++QTableView某一列用进度条显示
要在QTableView中的某一列中使用进度条显示,可以使用自定义委托方法来实现。以下是一个简单的示例:
首先,创建一个自定义委托类来显示进度条:
```cpp
class ProgressBarDelegate : public QStyledItemDelegate {
public:
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
if (index.data().canConvert<int>()) {
int progress = index.data().toInt();
QStyleOptionProgressBar progressBarOption;
progressBarOption.rect = option.rect;
progressBarOption.minimum = 0;
progressBarOption.maximum = 100;
progressBarOption.progress = progress;
progressBarOption.text = QString::number(progress) + "%";
progressBarOption.textVisible = true;
QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
} else {
QStyledItemDelegate::paint(painter, option, index);
}
}
};
```
然后,在使用QTableView显示数据之前,将该委托类应用于特定列:
```cpp
QTableView *tableView = new QTableView;
QStandardItemModel *model = new QStandardItemModel;
tableView->setModel(model);
// 添加数据
QList<QStandardItem*> rowItems;
rowItems << new QStandardItem("Item 1");
rowItems << new QStandardItem("Item 2");
rowItems << new QStandardItem("Item 3");
rowItems << new QStandardItem("50"); // 这里将进度值存储为字符串
model->appendRow(rowItems);
// 应用委托类
ProgressBarDelegate *delegate = new ProgressBarDelegate;
tableView->setItemDelegateForColumn(3, delegate); // 第4列显示进度条
tableView->show();
```
这样,在第4列中存储的整数值将被视为进度值,并以进度条形式显示。
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)