Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions 6_accessing_vector_attributes/6_accessing_vector_attributes.pro
Original file line number Diff line number Diff line change
@@ -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/
Expand All @@ -24,6 +29,7 @@ macx{
INCLUDEPATH = $${QGISDIR}/include/qgis \
$${QGISSRCDIR}/core \
$${QGISSRCDIR}/gui \
$${QGISSRCDIR}/analysis \
$${QGISSRCDIR}/plugins \
$${QGISSRCDIR}/providers \
$${QGISSRCDIR}/raster \
Expand Down
2 changes: 1 addition & 1 deletion 6_accessing_vector_attributes/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
65 changes: 30 additions & 35 deletions 6_accessing_vector_attributes/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@
//
#include <qgsapplication.h>
#include <qgsfeature.h>
#include <qgsfeatureattribute.h>
#include <qgsfield.h>
#include <qgsproviderregistry.h>
#include <qgsvectordataprovider.h>
#include <qgsvectorlayer.h>


MainWindow::MainWindow(QWidget* parent, Qt::WFlags fl)
MainWindow::MainWindow(QWidget* parent, Qt::WindowFlags fl)
: QMainWindow(parent,fl)
{
//required by Qt4 to initialise the ui
Expand All @@ -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);

Expand All @@ -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);
Expand All @@ -70,17 +69,17 @@ void MainWindow::addLayer()
}

//get the field list associated with the layer
std::vector<QgsField> myFields = mypLayer->fields();
QList<QgsField> 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
Expand All @@ -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<QVariant> 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);
}
}

2 changes: 1 addition & 1 deletion 6_accessing_vector_attributes/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down