QT C++ application using QGIS API - c++

I'm trying to develop a QT C++ application that uses QGIS API and I can't manage Qt Creator to compile the code. I've read several topicts at gis.stackexchange and from other sources. The information is mostly outdated and I havent't found a solution. What I need is an overall guideline about the compatibility of the following items that help me find the right combination:
Qt version: tried 4.8.6 and 5.9.
Qt compiler: tried MinGW, VS2008 Express (Qt 4.8.6) and VS Community 2017 (Qt 5.9)
QGIS version: tried 2.14 to 2.18.
QGIS compile: I only use the regular OSGeo4w installer and AFAIK QGIS is compiled with Qt4 and MSVC. I've tried to compile QGIS from source using MinGW with no success (again, versions compatibility issues).
Windows version: I'm using Windows 10 64bit.
OSGeo4W or OSGeo4W64.
Qt works Ok in every case with it's own libraries, but when I try to import QGIS libraries it gives an 'undefined reference' error when building, even though intellisense works fine as it recognizes QgsApplication as a member of qgsapplication.h.
The following is just a test I can't get working. My Pro file is:
QT += core gui xml
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QgisWindow
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
# QGIS
INCLUDEPATH += "C:/OSGeo4W64/apps/qgis-rel-dev/include"
INCLUDEPATH += "C:/OSGeo4W64/include"
LIBS += -L"C:/OSGeo4W64/apps/qgis-rel-dev/lib" -lqgis_core -lqgis_gui
DEFINES+=CORE_EXPORT=
DEFINES+=GUI_EXPORT=
# QGIS
main.cpp is:
#include <QApplication>
#include <qgsapplication.h>
int main(int argc, char *argv[])
{
QgsApplication app(argc, argv, true);
QgsApplication::setPrefixPath("C:/OSGeo4W64/apps/qgis-dev", true);
int retval = app.exec();
QgsApplication::exitQgis();
return retval;
}
Then it fails to build/compile whith this error:
main.cpp:20: error: undefined reference to `QgsApplication::QgsApplication(int&, char**, bool, QString const&, QString const&)'
I'm actually porting a PyQgis app I've made two years ago and as I'm fairly new to Qt Creator I'm stuck at he beginning.

I found the right combination of the above mentioned items to make it possible for Qt to import Qgis libraries and build the app:
Qt 4.8.6 for MSVC2010
Qt Creator 4.3.0 (from a Qt 5.9.0 installation). Qt Creator 3.0 may be used as well.
Visual C++ express 2010 - Include Windows Kits/SDK in install
OSGeo4W - Installed qgis-ltr-dev and all libraries
Cmake 2.8.2
To launch Qt Creator I use this .bat:
Creator.bat:
ECHO Setting up QGIS DEV ENV
set PYTHONPATH=
set OSGEO4W_ROOT=C:\OSGeo4W
call "%OSGEO4W_ROOT%\bin\o4w_env.bat"
#set QMAKESPEC=win32-msvc2010
#set PATH=%OSGEO4W_ROOT%\bin;%OSGEO4W_ROOT%\apps\qgis-ltr-dev\bin;%PATH%
#set INCLUDE=%INCLUDE%;%OSGEO4W_ROOT%\include;%OSGEO4W_ROOT%\apps\qgis-ltr-dev\include
#set LIB=%LIB%;%OSGEO4W_ROOT%\lib;%OSGEO4W_ROOT%\apps\qgis-ltr-dev\lib
path %OSGEO4W_ROOT%\bin;%SYSTEMROOT%\System32;%SYSTEMROOT%;%SYSTEMROOT%\System32\wbem;C:\Program Files (x86)\Git\bin;C:\Tools\QtCreator\bin;%PATH%
set VS100COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools
call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
start "Qt Creator" /B C:\Qt\Qt5.9.0\Tools\QtCreator\bin\qtcreator.exe %*
The PRO file:
QT += core gui xml
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = 1_hello_world_qgis_style
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp
## QGIS
INCLUDEPATH += "C:/OSGeo4W/include"
INCLUDEPATH += "C:/OSGeo4W/apps/qgis-ltr-dev/include"
LIBS += -L"C:/OSGeo4W/apps/qgis-ltr-dev/lib" -lqgis_core -lqgis_gui
DEFINES+=CORE_EXPORT=
DEFINES+=GUI_EXPORT=
## QGIS
main.cpp file (adapted from Tim Sutton's examples)
// Qt Includes
#include <QApplication>
// QGIS Includes
#include <qgsapplication.h>
#include <qgsproviderregistry.h>
#include <qgssinglesymbolrendererv2.h>
#include <qgsmaplayerregistry.h>
#include <qgsvectorlayer.h>
#include <qgsmapcanvas.h>
int main(int argc, char ** argv)
{
// Start the Application
QgsApplication app(argc, argv, true);
app.setPrefixPath("D:/GIS");
QString myPluginsDir = "<path to Qgis plugins dir>";
QString myLayerPath1 = "<path to shapefile 1>";
QString myLayerBaseName1 = "Layer1";
QString myLayerPath2 = "<path to shapefile 2>";
QString myLayerBaseName2 = "Layer2";
QString myProviderName = "ogr";
// Instantiate Provider Registry
QgsProviderRegistry::instance(myPluginsDir);
// create maplayer instances
QgsVectorLayer * mypLayer1 = new QgsVectorLayer(myLayerPath1, myLayerBaseName1, myProviderName);
QgsVectorLayer * mypLayer2 = new QgsVectorLayer(myLayerPath2, myLayerBaseName2, myProviderName);
QgsSingleSymbolRendererV2 *mypRenderer1 = new QgsSingleSymbolRendererV2(QgsSymbolV2::defaultSymbol(mypLayer1->geometryType()));
QgsSingleSymbolRendererV2 *mypRenderer2 = new QgsSingleSymbolRendererV2(QgsSymbolV2::defaultSymbol(mypLayer2->geometryType()));
QList <QgsMapCanvasLayer> myLayerSet;
mypLayer1->setRendererV2(mypRenderer1);
mypLayer2->setRendererV2(mypRenderer2);
// Add Vector Layers to the Layer Registry
QgsMapLayerRegistry::instance()->addMapLayer(mypLayer1, true);
QgsMapLayerRegistry::instance()->addMapLayer(mypLayer2, true);
// Add Layers to the Layer Set
myLayerSet.append(QgsMapCanvasLayer(mypLayer1, true));
myLayerSet.append(QgsMapCanvasLayer(mypLayer2, true));
// Create the Map Canvas
QgsMapCanvas * mypMapCanvas = new QgsMapCanvas(0, 0);
mypMapCanvas->setExtent(QgsRectangle(-63.50, -28.10, -58.33, -24.00)); // Chaco
mypMapCanvas->setWheelAction(QgsMapCanvas::WheelAction(2) , 1.2);
mypMapCanvas->enableAntiAliasing(true);
mypMapCanvas->setCanvasColor(QColor(255, 255, 255));
mypMapCanvas->freeze(false);
// Set the Map Canvas Layer Set
mypMapCanvas->setLayerSet(myLayerSet);
mypMapCanvas->setVisible(true);
mypMapCanvas->setWindowTitle("GIS DSH");
mypMapCanvas->refresh();
// Start the Application Event Loop
int retval = app.exec();
app.exitQgis();
return retval;
}
Paths are hardcoded (bad programming practice) but is just for this test.
And now it runs OK. Please, see my post on gis.stackexchange.com for the links and the screencaps of Qt Creator Build & Run configurations.

Related

How to enable making objects from QSctpSocket?

I am attempting to use QSctpServer & QSctpSocket, but I can't make an object from none of these classes.
What is the problem?
OS: Ubuntu 16.04
Qt version: 5.12.3
I also visited Qt stream control transmission protocol (sctp) but confused about what exactly should I do!
.pro is as follows:
QT += core
QT += network
TARGET = TcpServer
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
Compile Qt by enabling -sctp flag solved the problem. After goring to Qt source folder did the following:
./configure -sctp
make
make install

Openvino: Problem when trying to load CPU plugin in Qt

After installing and successfully run the OpenVino demos in my PC I start implementing a basic application in Qt with this library. I made the linking as Intel's documentation describes and the application successfully compiled.
auto plugin = PluginDispatcher({L""}).getPluginByDevice("CPU");
auto netBuilder = new CNNNetReader();
netBuilder->ReadNetwork("../TestModel/squeezenet1.1.xml");
netBuilder->ReadWeights("../TestModel/squeezenet1.1.bin");
auto network = netBuilder->getNetwork();
netBuilder->getNetwork().setBatchSize(1);
The application pops an exception when debugger reach getPluginByDevice call (getSuitablePlugin method from ie_plugin_dispacher.hpp (line 73)).
I am using MSVC2017 64bit with Qt 5.11.1 in a Windows 10 machine. The .pro file library call is:
#OpenVino
INCLUDEPATH += $$PWD/inference_engine/include
LIBS += -L$$PWD/inference_engine/lib/intel64/Release
LIBS += -linference_engine -llibiomp5md
Is anyone experienced the same or has an idea what's going on?
Thanks in advance,
Thanasis
The release libraries were causing the problem. When I switched to the debug ones (inference_engined.lib insted of inference_engine.lib) the application run successfully.
EDIT
I paste the code from .pro file in case someone face the same problem.
#OpenVino
INCLUDEPATH += $$PWD/inference_engine/include
CONFIG(release, debug|release):BuildVar=release
CONFIG(debug, debug|release):BuildVar=debug
equals(BuildVar,debug) {
message(Debug Mode)
LIBS += -L$$PWD/inference_engine/lib/intel64/Debug
LIBS += -linference_engined
}
equals(BuildVar,release) {
message(Release Mode)
LIBS += -L$$PWD/inference_engine/lib/intel64/Release
LIBS += -linference_engine
}

QT - PostgreSQL plugin error

I have an app, that should connect to database.
Under linux i've no problems, but under windows - > i've to add to system "Path" place, where QT enviroment is placed (C:/QT/Tools/QTCreator/bin) otherwise it doesn't work and shows errors like below.
id doesn't work if i place exe file on pendrive too
i would like to send this app to my firends, who shouldn't install QT environment and just run this app.
dirs on my pendrive
/
app.exe
plugins (dir)
-qsqlpsql.dll
-sqldrivers (dir)
--qsqlpsql.dll
what should i do? where did i mistake?
pro file:
QT += core gui sql
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = NZP
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
win32{
LIBS += -L"C:/Program Files/psqlODBC/0903/bin"
}
RC_FILE += Info.rc
in my main.cpp:
QApplication a(argc, argv);
qDebug()<<QCoreApplication::applicationDirPath();
a.addLibraryPath(QCoreApplication::applicationDirPath());
a.addLibraryPath(QCoreApplication::applicationDirPath()+"/plugins");
qDebug()<<a.libraryPaths();
MainWindow w;
w.show();
return a.exec();
if i run this app under windows, where i installed QT - everythink is correct, but if i move it on pendrive or run under machine without QT framework - i become error:
QSqlDatabase: QPSQL driver not loaded QSqlDatabase: available drivers: QSQLITE QODBC3 QODBC QPSQL QPSQL7
Failed to open database connection. Invalid driver specfied.
i have looked in internet, but by now without sensible solution
help!
Ugh! i found the problem!
1.
you have in your install destination this folder structure:
yourap.exe
-plugins -> folder
--sqldrivers -> folder
and you have to copy from migws/bin not QT/Tools/Creator/bin!
---qsqlpsql.dll
in your app directory you have to copy
libeay32.dll
with you found in:
C:\Program Files\psqlODBC\0903\bin (http://www.postgresql.org/ftp/odbc/versions/msi/ - ii used https://ftp.postgresql.org/pub/odbc/versions/msi/psqlodbc_09_03_0300.zip)
in your app (main.cpp), just add path:
QApplication a(argc, argv);
a.addLibraryPath(QCoreApplication::applicationDirPath()+"/plugins/");
a.addLibraryPath(QCoreApplication::applicationDirPath());

QAudioDecoder - no service found

I am trying to decode a .wav file using QAudioDecoder class. Even though I had included the QtMultimedia module into my .pro file by adding
QT += multimedia I am receiving an error that service for the QAudioDecoder was not found. I am not able to see where to problem lies.
I am using Qt 5.1.0 with MingGW 4.8 32 bit on Windows 7.
Error message:
defaultServiceProvider::requestService(): no service found for - "org.qt-project.qt.audiodecode"
.pro file:
QT += core
QT += multimedia
QT -= gui
TARGET = test
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
main file:
#include <QCoreApplication>
#include <QAudioDecoder>
#include <QAudioBuffer>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString pathToFile = "C:/Users/Mateusz/Documents/Szkola/Sound Processing/Lab1/artificial/easy/506Hz.wav";
QAudioDecoder decoder;
decoder.setSourceFilename(pathToFile);
decoder.start();
while(decoder.bufferAvailable()) {
QAudioBuffer buffer = decoder.read();
qDebug() << "Buffer size: " << buffer.byteCount();
}
return a.exec();
}
The Multimedia module uses plugins that are different on each platform (or compiler).
See http://qt-project.org/wiki/Qt_Multimedia_Backends
On Windows you have DirectShow and MediaFoundation (WMF).
Only the WMF plugin implements audio decoding features.
WMF plugin is only available with MSVC compiler.
See http://qt-project.org/doc/qt-5.1/qtmultimedia/platform-notes-windows.html
I had the same problem in Qt5.5 running under Linux.
The problem disappeared after upgrade to Qt5.5.1 using their MaintenanceTool.
I also struggled with this issue and finally got it to work by using the MS visual studio compiler in QT Creator, like Fernando Pelliccioni suggested.
Steps were:
-Use the Qt MaintenanceTool to add support for msvc2013
-Install Visual Studio 2013
-In Qt Creator go to Projects->Manage Kits and add msvc2013
-Build and run. Now QAudioDecoder works.

How to configure a fresh installation of qtcreator?

I'm trying to configue QtCreator with no success. I want to open an existing project. When I try to compile and run it, I get only empty black console. It looks like it compiles fine but no output.
I've installed this 32bit version:
Qt 5.2.0 for Windows 32-bit (MinGW 4.8, OpenGL, 689 MB) (Info)
When I start a new project, QtCreator wants me to run cmake. When It's done, it works fine. I can also build HelloWorld with "g++ main.cpp"
Can anyone tell me what should I do step by step? I ran out of strength. I don't know why it doesn't work.
I think it is not very important, but I'm using Win7 64bit. First, I tried to install 64bit QtCreator:
Qt 5.2.0 for Windows 64-bit (VS 2012, OpenGL, 589 MB) (Info)
But I had some problems with compilers. It looked like I didn't have them installed. I also tried an online installer and now this 32bit version. Always same result.
EDIT:
I found out something. This works:
ComputerGraphics.pro
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
INCLUDEPATH += C:/Users/user/Desktop/projekty/PocitacovaGrafika2/opencv/include
LIBS += -LC:\\Users\user\\Desktop\\projekty\\PocitacovaGrafika2\\opencv\\bin \
libopencv_core246d \
libopencv_highgui246d \
libopencv_imgproc246d \
SOURCES += main.cpp \
and main.cpp
#include <iostream>
int main(){
std::cout << "Hello";
return 0;
}
This even doesn't print Hello (.pro is the same as previously):
#include <iostream>
int main(){
std::cout << "Hello";
IplImage* img = cvLoad("Desert.jpg",1);
cvShowImage("img",img);
cvWaitKey(0);
return 0;
}
In debug, it complains:
The gdb process terminated unexpectedly (code 0)
During startup program exited with code 0xc0000135.
My solution:
1)
I've gone through my own tutorial on how to compile opencv for qt-creator with cmake here:
How to link opencv in QtCreator and use Qt library
(but using the latest opencv 2.4.8)
2)
I created a folder opencv on desktop.
I went into newly created opencv_bin/install and copied a folder include into the opencv on the desktop.
I continued into directory: install/x64/mingw and copied both bin and lib into opencv on the desktop.
3)
I moved the desktop/opencv directory into a working directory of my project.
i.e cut: desktop/opencv; paste: somepath/myQtProject/
4)
My .pro contains a reference like this:
INCLUDEPATH += C:/Users/user/Desktop/projekty/PocitacovaGrafika2/opencv/include
LIBS += -LC:\\Users\\user\\Desktop\\projekty\\PocitacovaGrafika2\\opencv\\bin \
libopencv_core248d \
libopencv_highgui248d \
libopencv_imgproc248d \
And in the main.cpp just #include "opencv2/opencv.hpp"
PS: I hope this will also help somebody else.