我正在使用QT Creator为QGIS Desktop 2.8.3(夜晚)编写插件 . 我的想法是获取当前项目中的图层名称列表,将它们放在列表中,每次单击一个项目时,插件都会获取属性表并将其放入表格小部件中 .

我想我几乎得到了所有这些,除了获得所有属性后,QGIS被抛弃了 .

这是我的插件图:mainapp == load => form1 == load => form2

作为一个插件,mainmodule有一个

QgisInterface* mIface

我从mainapp传递到form1并最终通过像这样的公共空间传递给form2

void form1::get_iface(QgisInterface* interface)
{
    mIface=interface;
}

然后,每次单击列表视图中的项目时,插件都将获取此属性:

void form02::get_attrib_table(QListWidgetItem *item)
{
    QString targetname=item->text();
    QStringList myString;
    int targetindex;
    for (int i=0;i<mIface->legendInterface()->layers().count();i++)
    {
        if (mIface->legendInterface()->layers().at(i)->name()==targetname)
        {
            targetindex=i;
            goto aab;
        }
    }
    aab:;

    QgsMapLayer* abc=mIface->legendInterface()->layers().at(targetindex);
    QgsVectorLayer* mypLayer = dynamic_cast<QgsVectorLayer*>(abc);
    //get attribute table format, ie. column headers
    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(mypLayer->pendingFields().count());
    ui->tableWidget->setRowCount(mypLayer->pendingFeatureCount());
    for (int i=0;i<mypLayer->pendingFields().count();i++)
    {
          myString<<mypLayer->pendingFields().field(i).name();
    }
    ui->tableWidget->setHorizontalHeaderLabels(myString);


///get feature data
if (mypLayer->featureCount()>0)
{
    int fcount=mypLayer->featureCount();
    int hcount=mypLayer->pendingFields().count();

    for (int p=0;p<fcount;++p)
    {
        mypLayer->select(p);
        for (int i=0;i<hcount;i++)
        {
            QTableWidgetItem *item=new QTableWidgetItem(mypLayer->selectedFeatures().at(p).attribute(i).toString());
            ui->tableWidget->setItem(p,i,item);
        }
        mypLayer->deselect(p);
    }
    delete mypLayer;
    delete abc;
}
}

毕竟我成功获得了所需的数据,但随后QGIS崩溃并创建了一个minidump . 我不确定我是否通过传递和使用这样的mIface来做正确的事情 .

另外,在另一个插件 when there is no layer loaded 中,我可以从文件加载图层并使用相同的代码获取属性 .

QGIS c插件上的材料很少,我还是新手 .