QTreeView作为项目最经常使用的空间,常用接口和操作必须熟悉熟悉在熟悉!!!
1、节点遍历
1 void ParamSettingDlg::GetNode() 2 { 3 for(int i = 0;i < model->rowCount() ;i++) 4 { 5 QStandardItem *item = model->item(i); 6 7 qDebug() <<"item = " << item->text(); 8 GetItem(model->item(i) ); 9 } 10 } 11 12 //GetItem 为递归函数,这样可以遍历到所有的节点 13 14 void ParamSettingDlg::GetItem(QStandardItem *item) 15 { 16 Q_ASSERT(item); 17 if(item->hasChildren()) 18 { 19 for(int i = 0;i < item->rowCount() ;i++) 20 { 21 QStandardItem * childitem = item->child(i); 22 qDebug() << "childitem = " << childitem->text(); 23 GetItem(childitem); 24 } 25 } 26 }
2、设置当前选中项
接口setCurrentIndex
1 for(int i = 0;i < standardModel_down->rowCount() ;i++) 2 { 3 QStandardItem *item = standardModel_down->item(i); 4 Q_ASSERT(item); 5 if(item->hasChildren()) 6 { 7 for(int i = 0;i < item->rowCount() ;i++) 8 { 9 QStandardItem * childitem = item->child(i); 10 qDebug() << "childitem = " << childitem->text(); 11 if(childitem->text() == fileName1) 12 { 13 ui->treeView_down->setCurrentIndex(childitem->index()); 14 } 15 } 16 } 17 qDebug() <<"item = " << item->text(); 18 }
3、树节点自定义样式
使用接口setIndexWidget,其实QTableWidget的setCellWidget借口内部也是调用 setIndexWidget。如下:
1 QStandardItem *children = new QStandardItem(); 2 QStandardItem *parent = model->item(r,c); 3 parent->appendRow(children); 4 5 QModelIndex index = model->indexFromItem(children); 6 ui->treeView->setIndexWidget(index,myLabel);
示例
1 Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) 2 { 3 ui->setupUi(this); 4 5 QStandardItemModel* model = new QStandardItemModel(); 6 ui->tableView->setModel(model); 7 ui->tableView->setFocusPolicy(Qt::NoFocus); //去掉选中单元格时的虚框 8 9 model->setHorizontalHeaderLabels({"姓名", "年龄", "性别"}); 10 model->setItem(0, 0, new QStandardItem("张三")); 11 model->setItem(0, 1, new QStandardItem("3")); 12 13 QComboBox* cmb = new QComboBox(); 14 cmb->addItems({"男", "女"}); 15 ui->tableView->setIndexWidget(model->index(0, 2), cmb); 16 17 model->setItem(1, 0, new QStandardItem("李四")); 18 model->setItem(1, 1, new QStandardItem("5")); 19 20 cmb = new QComboBox(); 21 cmb->addItems({"男", "女"}); 22 ui->tableView->setIndexWidget(model->index(1, 2), cmb); 23 }
4、数据检索
借助代理类QSortFilterProxyModel
1 m_model = new QStandardItemModel(ui->treeView);//模型 2 m_pFilterModel = new QSortFilterProxyModel(ui->treeView);//过滤模型 3 4 m_pFilterModel->setSourceModel(m_model); 5 m_pFilterModel->setFilterCaseSensitivity(Qt::CaseInsensitive); //大小写不敏感 6 m_pFilterModel->setAutoAcceptChildRows(true); //父项满足时不过滤子项 7 m_pFilterModel->setRecursiveFilteringEnabled(true); //递归匹配满足的子节点,父类可见 8 //指定初始化过滤列 9 m_pFilterModel->setFilterKeyColumn(0); 10 ui->treeView->setModel(m_pFilterModel);
5、获取当前选中项
通过QTreeView函数 currentIndex ()可以获取当前选中条目的QModelIndex,QModelIndex可以看做是QStandardItem的数据封装,知道 QModelIndex就可以知道QStandardItem,通过QStandardItemModel的 itemFromIndex 函数即可得到QModelIndex对应的QStandardItem。
如:
1 QStandardItemModel* model = static_cast< QStandardItemModel*>( ui-> treeView-> model()); 2 QModelIndex currentIndex = ui-> treeView-> currentIndex(); 3 QStandardItem* currentItem = model-> itemFromIndex( currentIndex );
标签:Qt,QTreeView,item,ui,new,model,treeView,节点,QStandardItem From: https://www.cnblogs.com/ybqjymy/p/18261233