Qt not working with VTK libraries - c++

Hi stackoverflow family
I'm trying to get vtk working with qt. I should admit that I'm a newbie, so go easy. I'm trying to learn c++ for myself and have chosen the following configuration to get me started;
OS: Windows 7 ultimate
toolkit: QT 5.3.2
IDE: QT Creator 3.2.1
VTK: 6.3.0
I downloaded and extracted the VTK to "C:\vtk-6.3.0" on my computer. I followed the instructions given on VTK's website to build VTK with cmake. I gave cmake the location of the source "C:/vtk-6.3.0" and specified a place to build the libraries "C:/vtk/bin".
Thinking everything had gone smoothly, I proceeded with the simplest example I could get away with, an example without a ui. I got the example from here
RenderWindowNoUiFile.cxx
#include <QApplication>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <QVTKWidget.h>
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QVTKWidget widget;
widget.resize( 256, 256 );
vtkSmartPointer<vtkSphereSource> sphereSource =
vtkSmartPointer<vtkSphereSource>::New();
vtkSmartPointer<vtkPolyDataMapper> sphereMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
sphereMapper->SetInputConnection( sphereSource->GetOutputPort() );
vtkSmartPointer<vtkActor> sphereActor =
vtkSmartPointer<vtkActor>::New();
sphereActor->SetMapper( sphereMapper );
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor( sphereActor );
widget.GetRenderWindow()->AddRenderer( renderer );
widget.show();
app.exec();
return EXIT_SUCCESS;
}
I added my .pro file manually
RenderWindowNoUiFile.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = RenderWindowNoUiFile
TEMPLATE = app
SOURCES += RenderWindowNoUiFile.cxx
INCLUDEPATH += C:\VTK\bin
I observed that qt has underlined all of the header files and when I ran the program and I got the error
Projects\RenderWindowNoUiFile\RenderWindowNoUiFile.cxx:3: error: vtkSmartPointer.h: No such file or directory
#include <vtkSmartPointer.h>
^
Please what I'm I doing wrong?
When I searched for any of the header files, I find each in a different location in the "C:\vtk\bin" or "C:\vtk-6.3.0" folder.
Please how do I solve this problem? Can I get all of the header files in a single folder so that I can refer to it in the .pro file?
For example if I can get all the header files in a directory called "C:\vtk\header", then I can refer to it in my .pro file as INCLUDEPATH += C:\vtk\header

You kind of touched on it with your comment "can I get all the headers in one directory" That is the setting INCLUDEPATH, but you have it set incorrectly.
There are a few issues in what you are doing.
First, your INCLUDEPATH is incorrect. It should be set to the "include" directory of the files that you've installed.
Second, you haven't included the library in your build- so even if your include files resolved, you would get link errors. This is because you haven't specified where the binary library files are.
Third, you didn't specify if your VTK build was static or dynamic. I'll assume this is dynamic. IF you don't know what this means, please look that up. The big difference is: do you need to also copy a .dll file to where your program is running from. If you have dynamic, the answer is yes.
About the comment a out the install target: with cmake you can set an option to change where it installs the binaries to. So the process goes like this with cmake:
1) Get the sources unpacked to a directory that you like.
2) Run cmake build folder (which you set to bin). What you did was confusing, because the build folder will contain everything: not just binaries, but headers and other install stuff. I usually call it "build"
3) Once cmake configures it may prompt for more configuration. This is where you may want to configure Qt for VTK. In this step you tell cmake where the Qt cmake folders are, so that you can configure VTK to build Qt stuff
4) Now you can change the default install path. By default, when you build the INSTALL project with visual studio (I'll describe below) it installs somewhere in program files. You can change this by setting CMAKE_INSTALL_PREFIX to whatever you want. For example: C:\VTK\install
5) Click generate to generate the visual studio project files or Make files, depending on your build configuration.
6) For visual studio: open the solution in the build directory and build all. When this completes, to install the files (#4) run the INSTALL project.
7) For Make files: run make (if you have multiple cores, use -jX where X is twice number of cores) then make install (don't use -j with make install)
Ok at this point you've compiled and installed the VTK distribution. Now you should fix your paths in your Qt .pro file.
For example:
INSTALLPATH+=C:/VTK/install/include
I don't know which libs you need, but below you should see the pattern:
LIBS+=-LC:/VTK/install/lib \
-lvtkRenderingCore-6.3
Now after this, since you have a dynamic build you need to copy the .dll files next to the exe file that you are creating from your Qt project.
Good luck!

If you use mingw with qt ,you have to compile source of vtk with mingw as you know.In that web page they use vs so I want to warn you about it.When you configure vtk source with cmake you should check "Module_vtkGUISupportQt" and "Module_vtkGUISupportQtOpenGL" options to able to work with qt.Also you have to do some config in Run & Build Options in Qt such as selecting compiler, kits etc.
Notice that choosing a compiler which is released after the release date of the library(in this case vtk) may prevent you from suffering of much errors.

Related

compiling CGAL draw_polygon and linking qt5

I am trying to learn meshing algorithm for CFD puposes and I found CGAL to be a good library to learn everything from ground. My vcpkg cgal[qt5] installation failes due to some problem in installing boost. But other library installation works. I also tried to follow this tutorial but OpenGR and libpointmatcher library generation fails and results cmake error. So I have to setup everything manually. I am using Visual Studio 2022 and Qt5.15.2. I have qt extension downloaded and configured in visual studio. I have also QTDIR variable created in enviroment variables and Qt bin directory in system path.
What I did is as follows-
(1). first I created an empty console application called polygon and added various example code provided by cgal as a source and configuration x64, Release. changed
(2). created a folder library inside solution directory and extracted downloaded cgal-5.5.zip(has include, data, demo, etc ....) to library directory and added $(SolutionDir)library\cgal-5.5\include to additional include path in property manager.
(3). downloaded boost_1_80 from boost.org and compiled with the foolowing steps-
.\bootstrap
open project-config.jam and add-
using mpi ;
using python ;
run the command-
.\b2 --build-dir=build\x64 address-model=64 threading=multi --stagedir=stage/x64 --toolset=msvc -j 16 link=static,shared runtime-link=static,shared --variant=debug,release
added the boost_1_80 folder in include path and stage/x64/lib to additional library directory.
(4). I have built gmp and mpfr with vcpkg with .\vcpkg install gmp:x64-windows-static & .\vcpkg install mpfr:x64-windows-staticcommand and copied the folders gmp_x64-windows-static and mpfr_x64-windows-static to the library directory. I added include and lib folder of both library to respective path and in linker->additional dependency included the two lib file gmp.lib and mpfr.lib
(5). copied images, meshes, points_3 folder from data to solution directory
I can add eigen3, zlib to my project using the same process and all the examples I tried depend on the above libraries compiled and ran properly.
The problem comes when I try to do examples involves qt5. For example program draw_polygon.c-
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polygon_2.h>
#include <CGAL/draw_polygon_2.h>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Polygon_2<K> Polygon_2;
typedef CGAL::Point_2<K> Point;
int main()
{
// create a polygon and put some points in it
Polygon_2 p;
p.push_back(Point(0,0));
p.push_back(Point(4,0));
p.push_back(Point(4,4));
p.push_back(Point(2,2));
p.push_back(Point(0,4));
CGAL::draw(p);
return EXIT_SUCCESS;
}
I tried following this tutorial but it's made for older version of Qt(Qt4) and Qt5 does not have Qt folder(..\path to\Qt<QT-version>\include) inside includes. This program does not any form file but I added the rest of the includes and lib files accordingly. When I try to compile, it says CGAL_USE_BASIC_VIEWER not defined. Given in this article if I write #define CGAL_USE_BASIC_VIEWER before the includes the generated are like -
Then I tried doing from qt empty project in visual studio interface which still gives -
Obviously my linking failed. How do I fix it for my case? I have went through several other articles none had definite answers.

Where do I put the QT5 clone in this project

This is my first time every using C++ so please go easy on me. I have about 7000 hours of Python experience so I'm not completely clueless. I'm trying to read the code written for the Collatinus software found here. collatinus. It seems that the initial file is this:
VERSION = "11.2"
DEFINES += VERSION=\\\"$$VERSION\\\"
DEFINES += MEDIEVAL
TEMPLATE = app
TARGET = collatinusd
INCLUDEPATH += . src
DEPENDPATH += .
DESTDIR = bin
OBJECTS_DIR= obj/
MOC_DIR = moc/
QMAKE_DISTCLEAN += $${DESTDIR}/collatinus
CONFIG += console
CONFIG -= app_bundle
CONFIG += release_binary
QT += core
QT -= gui
QT += xmlpatterns
QT += network
I've downloaded qt from qt and got the open source qt5. Now I cannot figure out where to put the file. I have tried putting it in the same folder as the above mentioned code. I have also tried putting in the folder marked src. In the src folder there are many files which use QT but it seems like the files are supposed to be taken out of the QT folder. For example in this file we have the syntax on line 28
#include <QDebug>
The QDebug file is in the QT folder. But when I put the qt folder in either the topmost folder or the src folder I get the error message:
fatal error: 'QtCore' file not found
#include <QtCore>
So the file structure is as follows:
/collatinus-daemon
collatinus.pro (and other files)
//src
flexion.cpp (and other files)
So where do I put the qt folder? Also, I renamed it qt from qt5 since the syntax had the line:
QT += core
###################
Ok, I've got the QT creator up and running. Here is the pro file
QT += network widgets
QT += core
QT -= gui
TARGET = Client_C11
VERSION = "1.0"
#CONFIG += console
#CONFIG -= app_bundle
CONFIG += release_binary
TEMPLATE = app
SOURCES += src/client_main.cpp
OBJECTS_DIR= obj/
MOC_DIR = moc/
unix:!macx:DESTDIR = bin
macx:{
# Commandes spéciales pour déployer l'application sur Mac.
# J'ignore s'il faut l'équivalent pour Linux ou Windows.
# Philippe. Octobre 2016
QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.8
ICON = collatinus_bw.icns
deploy.commands = macdeployqt Client_C11.app
QMAKE_EXTRA_TARGETS += deploy
}
Here are some screenshots of my folders:
And here is the error message I'm getting:
Here also is the code for the client main
#include <QCoreApplication>
#include <iostream>
#include <QtWidgets>
#include <QtNetwork>
class QTcpSocket;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString req = "";
if (argc > 1)
{
int i = 1;
while (i < argc)
{
QString suite(argv[i]);
req += " " + suite;
i++;
}
}
else req = "-?"; // pour afficher l'aide.
QTcpSocket * tcpSocket = new QTcpSocket();
tcpSocket->abort();
tcpSocket->connectToHost(QHostAddress::LocalHost, 5555);
QByteArray ba = req.toUtf8();
tcpSocket->write(ba);
tcpSocket->waitForBytesWritten();
tcpSocket->waitForReadyRead();
ba = tcpSocket->readAll();
tcpSocket->disconnectFromHost();
tcpSocket->close();
QString rep(ba);
std::cout << rep.toStdString();
a.quit();
}
I don't understand what I'm supposed to do with qmake and cmakelists
Where do I put the QT5 clone in this project
You don't :)
Qt installations up to 5.14 are not relocatable. That means that once Qt is installed, if you move it to another path, it'll break. Just don't mess with it: once installed, you leave it alone, and it'll work just fine.
Qt source code has to be built before it can be used. I presume that you downloaded the source code and want to stick it into the project and build the two that way. It's not designed to work that way at all.
Qt build requires several other tools to be installed (iirc python, ruby, perl), and other optional dependencies to get the full feature set, and if it fails for whatever reason, figuring it out is unnecessary effort initially. So it's best to start with pre-built Qt.
I've downloaded qt from qt and got the open source qt5. Now I cannot figure out where to put the file.
Downloading "qt" doesn't mean much, since everyone means something else by that. If you've downloaded the source code, then delete it - last thing you want is to mess with compiling Qt right now.
Normally, Qt is installed using an installer, so what you'd download is an executable installer, and use that to select the Qt components you want to install. On Windows, pick the mingw-based Qt version, since that also installs the build environment (compilers) if you don't have them already. Otherwise you'd use the MSVC-based version if you got MSVC installed on Windows.
On Unix, you'd want to install Qt using the "native" package manager - one that came with your linux distribution, or macports (really preferred) on MacOS.
Once Qt is installed, you'd use Qt Creator IDE to open the collatinus project (its .pro file). And everything will "just work" from that point onwards - it'll let you build it and run whatever executable targets it produced. Of course you can build from the command line, but for a beginner it's just an extra layer of complication and unnecessary.
In any case, the .pro file you refer to would be processed by qmake to generate the build system that builds the project. qmake itself is the means you use to select what Qt version you build with: there's one qmake per each Qt installation. So, after qmake has ran, you'd make the thus-configured build, and there'll be no problems with finding Qt headers. It's the job of qmake in that case to set everything up so that the compiler will be told where to find Qt. You are not expected to have to mess with it manually.
If there's a CMakeLists.txt file in the project, you'd probably prefer to use that instead of qmake, since cmake is a widely used tool with lots of knowledge available online, whereas qmake is now obsolete. Still, older projects may only supply a .pro file that needs to be used with qmake and not cmake.
Your question doesn't nearly provide enough detail for a more focused answer - please tell us exactly what you did, and what Qt elements you installed (whether using Qt Installer program, or using a unix package manager).

Build c++ Qt CLI Tool with Quazip

so normally I am an embedded software developer first time I need to do something like that... So I am no expert at all at this... I have come this far with a lot of googling and trial and error methods...
What I want to do
I need to write a C++ CLI application which can zip and unzip files.
Quazip
zlib
My general conditions
I want to use Qt and Quazip since Qt doesn't support zipping natively (or does it).
I want to build with Mingw and CMake for Windows 64bit on a Windows 64bit system.
With my knowledge, it would be best if I compile Quazip statically so I don't depend on any other quazip DLL which may be present on other systems...
How I think It works
I need to compile zlib
Then I need to compile Quazip (with zlib)
Link the compiled libraries to my project.
My research
I found two good youtube videos which should help me:
https://www.youtube.com/watch?v=5ZTusMX-Reo
https://www.youtube.com/watch?v=mxlcKmvMK9Q
No of these ways did work out for me.
Also I found that blog post which was a little bit of help...
http://www.antonioborondo.com/2014/10/22/zipping-and-unzipping-files-with-qt/
This site was available till yesterday now it's gone :D
Also I found that old Stackoverflow question which helped me a bit:
How to connect the QuaZip library in CMake
Detailed steps on what I did:
1. Building zlib
Downloaded the latest version of zlib from the official website. version 1.2.11
I build zlib with the following commands:
cd C:/some/Path/zlib-1.2.11
mingw32-make -f win32/Makefile.gcc
That works perfectly I get a
libz.a
libz.dll.a
zlib1.dll
those files and all the header files (11 files) I copy in a seperate folder. Which looks like this:
Build
|
+-include
+-lib
Headers in the include folder the libs in the lib folder.
2. Building Quazip
Now the messy part...
Downloading the newest version of Quazip (version 0.8.1) from the GitHub.
I tried various ways to compile Quazip. The one I stick to was:
Open the .pro file in Qt creator inside the quazip folder (the folder where the sources are located).
So then I added the compiled zlib to the Qmake file.
Following lines I added at the end of the file:
INCLUDEPATH += "C:/Build/include"
LIBS += -L"C:/Build/lib"
LIBS += -lz
Then build Quazip with QtCreator as release Build.
When compiled I get
quazip.dll
libquazip.a
I copy all the header files from quazip (16 fils) in the include folder and the two libs in the lib folder.
Now I have like a zippackage in the build folder.
3. Adding Quazip and zlib to my Project
Copied the FindQuazip.cmake file into my project form the Quazip Repo.
In my CMakeLists.txt I added the following lines:
set(ZLIB_ROOT "C:/BUILD" CACHE PATH "Path to zlib")
find_package(ZLIB REQUIRED)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
find_package(QuaZip REQUIRED)
I need to set the ZLIB_ROOT so CMake uses the zlib I want. If I don't set Root he uses a zlib from a ruby installation from my PC.
Also I added
include_directories(
"${BASE_DIR}/sources"
"${QUAZIP_INCLUDE_DIR}"
)
and at the end
add_executable(${PROJECT_NAME} ${SOURCES} ${INC_ALL} )
target_link_libraries(${PROJECT_NAME} Qt5::Network Qt5::Core Qt5::Widgets
${ZLIB_LIBRARIES}
${QUAZIP_LIBRARIES}
)
Then I needed to add the FindQuazip.cmake since it would find the libs: I edited the following:
FIND_PATH(QUAZIP_LIBRARY_DIR
WIN32_DEBUG_POSTFIX d
NAMES libquazip.a #quazip.dll
HINTS "C:/Build/lib"
PATH_SUFFIXES QuaZip/lib
)
FIND_LIBRARY(QUAZIP_LIBRARIES NAMES libquazip.a HINTS ${QUAZIP_LIBRARY_DIR})
FIND_PATH(QUAZIP_INCLUDE_DIR NAMES quazip.h HINTS ${QUAZIP_LIBRARY_DIR}/../include)
FIND_PATH(QUAZIP_ZLIB_INCLUDE_DIR NAMES zlib.h)
Okay so It took me two days to get to there where I am now.
Now when I run CMake every works fine. He finds the libs.
I can even include a the Header files of Quazip. But when I then try to compile a minimal example the linker can't find any symbols.
Minimal Example:
#include <iostream>
#include <QApplication>
#include "JlCompress.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
cout << __cplusplus << endl;
cout << "Hello World!" << endl;
JlCompress::compressDir("C:/WorkspaceLokal/Test/test.zip", "C:/WorkspaceLokal/Test/TestFolder");
return a.exec();
}
By compiling it I get the following error:
C:\some\path\sources\main.cpp:-1: Fehler: undefined reference to `JlCompress::compressDir(QString, QString, bool)'
This means the linker can't link the function...
My Question
What am I doing wrong?
Do I need to compile Quazip in another way?
Edit 4:
since it now works how to I compile the Quazip as a static lib, so that I can link it statically to my application?
Thanks in advance...
Edit 1:
Here are the QUAZIP variables from Qt creator:
Edit 2:
Okay. Some news. the libquazip.a is something else and doesn't work. If I link against the quazip.dll compiling works. But when I run the application it crashes at the function call compressDir...
Edit 3:
It works. I just needed to copy the quazip.dll to the compiled executable...
From QuaZip's pro file:
# You'll need to define this one manually if using a build system other
# than qmake or using QuaZIP sources directly in your project.
CONFIG(staticlib): DEFINES += QUAZIP_STATIC
This should trigger when you add staticlib to the CONFIG in quazip.pro (2nd line):
CONFIG += qt warn_on staticlib
If you are not using qmake to build Quazip, just make sure that you #define QUAZIP_STATIC in a way specific to your build system

What am I doing wrong with setting up ITK library for Qt Creator using CMake?

I have tried both:
I built ITK-library using CMake and then linked my HelloWorld.cxx manually in the .pro file as shown here:
INCLUDEPATH += "/folder1/folder2/folder3/ITK-build/lib"
LIBS += /folder1/folder2/folder3/ITK-build/lib/
-libITKBiasCorrection-4.7/
-libITKBioCell-4.7/
-libITKCommon-4.7/
-libitkdouble-conversion-4.7/
-libITKDICOMParser-4.7/
etc.
However when I build this in Qt Creator it does not recognize #include "itkImage.h" and thus does not compile...
I wrote an CMakeLists.txt files and called it in Qt Creator using File > Open File or Project. My CMakeLists.txt looks like this:
PROJECT(HelloWorld)
FIND_PACKAGE (ITK REQUIRED)
IF (ITK_FOUND)
INCLUDE( ${ITK_USE_FILE} )
ENDIF(ITK_FOUND)
ADD_EXECUTABLE(HelloWorld HelloWorld.cxx)
TARGET_LINK_LIBRARIES (HelloWorld /folder1/folder2/folder3/ITK-build/lib)
My HelloWorld.cxx looks as follows (from a given example from www.itk.org):
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkGradientMagnitudeImageFilter.h"
int main( int argc, char **argv) {
typedef itk::Image <unsigned short,2> ImageType;
typedef itk::ImageFileReader <ImageType> ReaderType;
typedef itk::GradientMagnitudeImageFilter <ImageType,ImageType> FilterType;
ReaderType::Pointer reader = ReaderType::New();
FilterType::Pointer filter = FilterType::New();
reader->SetFileName(argv[1]);
filter->SetInput(reader->GetOutput() );
filter->Update();
return 0;
}
What am I doing wrong? Thanks for any help.
I might be wrong but...
You are saying no include inside the ITK-build folder and that you are just trying to grab whats inside the build folder. Check if ITK has a make install (I'm not familiar with ITK but I have successfully built and installed VTK by Kitware who currently manages both ITK and CMake). Otherwise you will have to do a combo between the sources and built folders to get all that you need.
In most cases it's the installation step that creates the final "image" of the software that you use in your own projects. This step copies library files, executables (usually examples and/or tests), headers etc. to the installation path. In case of ITK it also should place the required cmake modules so that you can actually use find_package(ITK). Some developers prefer to copy the required headers first to the build folder and then to the installation path. There isn't a "standard" way of doing this.
I also suggest to output some message inside the if-check
IF (ITK_FOUND)
message("ITK here!!!")
INCLUDE( ${ITK_USE_FILE} )
ENDIF(ITK_FOUND)
in order to see if the include(...) is executed at all. My guess would be that it's not.

Qt Libraries cannot be found on Mac

I'm having issues setting up the Qt environment on Mac 10.9.1. If I just try to compile a C++ file with the standard g++ source.cpp -o output Then none of the Qt Libraries are found. For instance if I have
#include <QString>
Then I will get the error fatal error: 'QString' file not found
I have installed Qt 5.2.1 and added it to my PATH variable, so now when i make the project using qmake -project, qmake -spec macx-g++ and then make
I get the error saying that my version of Mac OSX is not supported. I have to use Qt for my college assignment, please can someone help me set this up.
Any help would be appreciated.
Mavericks is definitely supported by Qt 5. It is not supported by Qt 4 so far; trojanfoe made a typo in his comment (nomen est omen?).
Your mistake is using the wrong make spec. Qt 5 uses clang, not gcc. Thus the following works for me (without setting any paths):
~/Qt5.2.1/5.2.1/clang_64/bin/qmake -project
~/Qt5.2.1/5.2.1/clang_64/bin/qmake -spec macx-clang
make
You can have multiple Qt versions existing side by side, there's no reason to uninstall anything when you get a new Qt version installed.
A small self-contained example would be below. Put it in a simple folder. To build, do:
~/Qt5.2.1/5.2.1/clang_64/bin/qmake -spec macx-clang
make
You do not want to regenerate the .pro file by invoking qmake with -project argument. The project generation is just to give you a simple skeleton, you're only supposed to do it as a convenience when importing third-party code.
Note that by definition if you use any visible GUI elements (windows, message boxes, etc), it's not a console application anymore as far as Qt is concerned.
# simple.pro
TEMPLATE = app
QT += widgets
# Creates a simple executable instead of an app bundle
CONFIG -= app_bundle
SOURCES += main.cpp
// main.cpp
#include <QApplication>
#include <QMessageBox>
int main(int argc, char ** argv)
{
// It is an error not to have an instance of QApplication.
// This implies that having an instance of QCoreApplication and QGuiApplication
// is also an error.
QApplication app(argc, argv);
QMessageBox::information(NULL, "Get This!", "Something's going on");
}