构造属性表
方法来自:
https://www.cnblogs.com/rainbow70626/p/10382226.html
最开始要做的是用 QgsMapCanvas 初始化编辑控件
QgsEditorWidgetRegistry::initEditors( m_mapCanvas );
然后再开始构造并显示属性表格。 属性表格自然需要一个矢量图层 然后将它 Cache 进 QgsVectorLayerCache 类中以便用于构造QgsAttributeTableModel类。
QgsVectorLayerCache* lc = new QgsVectorLayerCache( m_buildingLayer, m_buildingLayer->featureCount() );
接着分别创建 QgsAttributeTableView 和 QgsAttributeTableModel 用于显示属性表格
QgsAttributeTableView* tv = new QgsAttributeTableView();
QgsAttributeTableModel* tm = new QgsAttributeTableModel( lc );
构造好 QgsAttributeTableModel 一定不要忘记加载一下图层,构造函数没有默认完成这个工作。
tm->loadLayer();
现在构建好的 QgsAttributeTableModel 不能直接与 QgsAttributeTableView 进行绑定,而是需要一个 Filter 在中间做“筛选”,因此需要一个 QgsAttributeTableFilterModel 来建立 QgsAttributeTableModel 和 QgsAttributeTableView 的之间联系。当然,不要忘记构造 QgsAttributeTableFilterModel 类还需要一个 QgsMapCanvas。
QgsAttributeTableFilterModel* tfm = new QgsAttributeTableFilterModel( m_mapCanvas, tm, tm );
tv->setModel( tfm );
最后,将属性表格显示出来就好了
tv->show();
feature添加属性
const QgsFields &fields=vectorLayer->pendingFields();
QgsFeature f(fields,nbuildingID);
f.setGeometry(polyafterdelete);
nbuildingID++;
//f.initAttributes(fields.count());
f.setAttribute( "id", nbuildingID );
f.setAttribute( "Length", polyafterdelete->length() );
vectorLayer->addFeature(f);
feature更改,属性更改
vectorLayer->changeAttributeValue(f.id(),1,fgeo->length());//其中1代表“Length”的field序号
vectorLayer->changeGeometry(f.id(),fgeo);
一些网址
向shp矢量图层中添加新的字段:
https://www.cnblogs.com/rainbow70626/p/10382226.html
https://blog.csdn.net/qq_26212909/article/details/60142017
添加矢量要素,其中addfeature函数中有添加属性部分:
https://blog.csdn.net/shineplusplus/article/details/78362085