From dd427dbe24d64a55525c641bee4156374c72c4ec Mon Sep 17 00:00:00 2001 From: Thomas Gratier Date: Sat, 10 Apr 2021 14:14:49 +0200 Subject: [PATCH] Update sample 6 for QGIS 3.x --- .../6_accessing_vector_attributes.pro | 10 ++- 6_accessing_vector_attributes/main.cpp | 2 +- 6_accessing_vector_attributes/mainwindow.cpp | 65 +++++++++---------- 6_accessing_vector_attributes/mainwindow.h | 2 +- 4 files changed, 40 insertions(+), 39 deletions(-) diff --git a/6_accessing_vector_attributes/6_accessing_vector_attributes.pro b/6_accessing_vector_attributes/6_accessing_vector_attributes.pro index 8e7e26c..6685e75 100644 --- a/6_accessing_vector_attributes/6_accessing_vector_attributes.pro +++ b/6_accessing_vector_attributes/6_accessing_vector_attributes.pro @@ -1,14 +1,19 @@ TEMPLATE = app TARGET = qgis_example6 -QT = qt3support sql opengl network svg gui core xml +QT = sql opengl network svg gui core xml +#QMAKE_CC = gcc-8 +#QMAKE_CXX = g++-8 LANGUAGE= C++ linux-g++{ - QGISDIR=[path to installed qgis] + QGISDIR=/usr QGISLIBDIR=$${QGISDIR}/lib QGISSRCDIR=[path to qgis src directory] QGISPLUGINDIR=$${QGISLIBDIR}/qgis DEFINES += QGISPLUGINDIR=$${QGISPLUGINDIR} LIBS = -L$${QGISLIBDIR} -lqgis_core -lproj -lqgis_gui + # Below was better working on my machine due to error in linker e.g + # undefined reference to `std::thread::_State::~_State()@GLIBCXX_3.4.22' + # LIBS = -L$${QGISLIBDIR}/x86_64-linux-gnu -lqgis_core -lproj -lqgis_gui } macx{ QGISDIR=/Users/timsutton/apps/qgis.app/Contents/MacOS/ @@ -24,6 +29,7 @@ macx{ INCLUDEPATH = $${QGISDIR}/include/qgis \ $${QGISSRCDIR}/core \ $${QGISSRCDIR}/gui \ + $${QGISSRCDIR}/analysis \ $${QGISSRCDIR}/plugins \ $${QGISSRCDIR}/providers \ $${QGISSRCDIR}/raster \ diff --git a/6_accessing_vector_attributes/main.cpp b/6_accessing_vector_attributes/main.cpp index 3285180..4c46988 100644 --- a/6_accessing_vector_attributes/main.cpp +++ b/6_accessing_vector_attributes/main.cpp @@ -30,7 +30,7 @@ int main(int argc, char ** argv) { // Start the Application - QgsApplication app(argc, argv, TRUE); + QgsApplication app(argc, argv, true); MainWindow * mypMainWindow = new MainWindow(); mypMainWindow->show(); // Start the Application Event Loop diff --git a/6_accessing_vector_attributes/mainwindow.cpp b/6_accessing_vector_attributes/mainwindow.cpp index 73a7798..8daf4f7 100644 --- a/6_accessing_vector_attributes/mainwindow.cpp +++ b/6_accessing_vector_attributes/mainwindow.cpp @@ -23,14 +23,13 @@ // #include #include -#include #include #include #include #include -MainWindow::MainWindow(QWidget* parent, Qt::WFlags fl) +MainWindow::MainWindow(QWidget* parent, Qt::WindowFlags fl) : QMainWindow(parent,fl) { //required by Qt4 to initialise the ui @@ -40,7 +39,7 @@ MainWindow::MainWindow(QWidget* parent, Qt::WFlags fl) #if defined(Q_WS_MAC) QString myPluginsDir = "/Users/timsutton/apps/qgis.app/Contents/MacOS/lib/qgis"; #else - QString myPluginsDir = "/home/timlinux/apps/lib/qgis"; + QString myPluginsDir = "/usr/lib/qgis"; #endif QgsProviderRegistry::instance(myPluginsDir); @@ -53,8 +52,8 @@ MainWindow::~MainWindow() void MainWindow::addLayer() { - QString myLayerPath = "data"; - QString myLayerBaseName = "test"; + QString myLayerPath = "/home/thomasg/ne_10m_admin_0_countries.shp"; + QString myLayerBaseName = "Countries"; QString myProviderName = "ogr"; QgsVectorLayer * mypLayer = new QgsVectorLayer(myLayerPath, myLayerBaseName, myProviderName); @@ -70,17 +69,17 @@ void MainWindow::addLayer() } //get the field list associated with the layer - std::vector myFields = mypLayer->fields(); + QList myFields = mypLayer->fields().toList(); //we will hold the list of attributes in this string QString myString; //print out the field names first - for (unsigned int i = 0; i < myFields.size(); i++ ) + for (int i = 0; i < myFields.size(); i++ ) { //a little logic so we can produce output like : // "foo","bar","etc" if (i==0) { - // here is where we actually get the field value + // here is where we actually get the field value myString = "\"" + myFields[i].name().trimmed() + "\""; } else @@ -91,38 +90,34 @@ void MainWindow::addLayer() textBrowser->append("Field List: " + myString); //get the provider associated with the layer //the provider handles data io and is a plugin in qgis. - QgsVectorDataProvider* mypProvider=mypLayer->getDataProvider(); + QgsVectorDataProvider *mypProvider=mypLayer->dataProvider(); //check the provider is valid - if(mypProvider) + QgsFeatureIterator fi = mypProvider->getFeatures(); + + //create a holder for retrieving features from the provider + QgsFeature mypFeature; + while ( fi.nextFeature( mypFeature ) ) { - //create a holder for retrieving features from the provider - QgsFeature *mypFeature; - //get the provider ready to iterate through it - mypProvider->reset(); - //now iterate through each feature - while ((mypFeature = mypProvider->getNextFeature(true))) + + //get the attributes of this feature + QVector myAttributes = mypFeature.attributes(); + //now loop through the attributes + for (int i = 0; i < myAttributes.count(); i++) { - //get the attributes of this feature - const std::vector < QgsFeatureAttribute >& myAttributes = mypFeature->attributeMap(); - //now loop through the attributes - for (int i = 0; i < myAttributes.size(); i++) + QString val = myAttributes[i].toString().trimmed(); + // qDebug("%s", val.trimmed().toUtf8().constData()); + //a little logic so we can produce output like : + // "foo","bar","etc" + if (i==0) { - //a little logic so we can produce output like : - // "foo","bar","etc" - if (i==0) - { - // here is where we actually get the field value - myString = "\"" + myAttributes[i].fieldValue().trimmed() + "\""; - } - else - { - myString += ",\"" + myAttributes[i].fieldValue().trimmed() + "\""; - } + // here is where we actually get the field value + myString = "\"" + val + "\""; + } + else + { + myString += ",\"" + val + "\""; } - textBrowser->append("Field Values: " + myString); - //clean up before moving on to the next feature - delete mypFeature; } + textBrowser->append("Field Values: \n" + myString); } } - diff --git a/6_accessing_vector_attributes/mainwindow.h b/6_accessing_vector_attributes/mainwindow.h index 3f4b488..772ac40 100644 --- a/6_accessing_vector_attributes/mainwindow.h +++ b/6_accessing_vector_attributes/mainwindow.h @@ -33,7 +33,7 @@ class MainWindow : public QMainWindow, private Ui::MainWindowBase { Q_OBJECT; public: - MainWindow(QWidget* parent = 0, Qt::WFlags fl = 0 ); + MainWindow(QWidget* parent = 0, Qt::WindowFlags fl = 0 ); ~MainWindow(); public slots: void addLayer();