Using Qt5 in a library - c++

I have a library that integrates some GTK functionnalities, and I decided to switch to Qt.
So I created a test cmake file to try to integrate Qt5 but it does not work because when I load the libary dynamically using an executable I get "undefined symbols".
undefined symbol: _ZN8UIWindow16staticMetaObjectE
I googled a lot, and it does not seems to be that simple si any kind of help, or comments would be much appreciated.
Here are the relevant files I use.

Ok I found the solution. Since I use different folders for the header and the source files, I have to manually specify where is the file containing the qt code.
QT5_WRAP_CPP( MOCS_HDRS ${INCLUDE_DIR}/UIWindow.h )

The error indicates that the MOC file for your UIWindow class was not generated or built correctly. Qt expects certain meta-information for the class generated by MOC (MetaObject Compiler, IIRC).
Your CMake script turns on automoc which usually works, so I suggest you take a look at the output of CMake to see if it is running that step for your class or not.

Related

Class QSqlError has no member nativeErrorCode()

I have been struggling with this problem for quite a while, and unfortunately neither my own reasoning, nor google search helped me. In simple terms, I am trying to use nativeErrorCode() function with QSqlError class. Compiler says: mainwindow.cpp:43:86: error: ‘class QSqlError’ has no member named ‘nativeErrorCode’. This is the line that it references (to be exact, two lines):
errorCreatingBooksTableMessageBox.setDetailedText((query.lastError().nativeErrorCode().isEmpty()) ?
query.lastError().text() : query.lastError().nativeErrorCode() + "\n" + query.lastError().text());
Problem seems trivial, but I can't find an answer on my own. #include is in mainwindow.h file. After some research, I noticed other people on other forums suggesting that compiler uses wrong version of the header file, which would seem very likely to happen since function nativeErrorCode() was introduced in Qt5. I tried to move qt4 folder to trash, but still, no effect. Any help would be appriciated
I am not sure which OS you encounter this issue. However In most of the Linux OS Image, prebuild with Qt4, Even we build Qt5 libraries additionally, the system environment may use old Qt4 libraries to generate make files. This is very common if we are building Qt5 from source codes. So it would be better you can try to execute qmake from Qt5 path. So the Makefile will be created to build for Qt5 libraries.
In order to confirm, Open the Makefile and check the variable QMAKE is refering to Qt5 or Qt4 libraries.
I am updating this from my system for your reference, "QMAKE = /home/user1/Qt5.6.0/5.6/gcc_64/bin/qmake" From this, It is clear that Makefile is using Qt5 libraries and further compilation, Qt5 libraries will be used to compile my application source code.

Add external library into current c++ project

I'm trying to use the xgboost library to basically translate a model I developed in R to C++.
I've not used an external library and other sources online did not help me much so decided to ask the question here to hopefully find the way to do it.
Here's the library I need to use in my C++ code: https://github.com/dmlc/xgboost
I'm using Visual Studio Ultimate 2013. I have a make file that I use to define my project.
I am looking for the proper way to use this library in my C++ code.
If you have ever used this library before, I really appreciate your help. Any hint will be appreciated.
To use the library, one needs to first build it. I could not manage to build the master branch. So, I decided to use the V0.4 instead. I built the solution in that branch using Visual Studio and it produced the dll and lib files. One can then import the .dll file by using Add_library to add it via Cmake. After Add_library, the include directories need to be added by include_directories command.
I need to generate .a and .so files in the end. I'm working in windows and could not find a way to generate these files, yet!
So I found the way to resolve this issue. I shared it in another answer
Add an external library (e.g., Eigen) via CMake
Hope this helps people who are looking!

QT + CImg will not build on OSX 10.9

I'm taking over a project where I need to work with QT Creator and CImg, however it won't build on my machine.
/Users/vikkosmi/Qt/5.2.0/clang_64/lib/QtCore.framework/Headers/qvariant.h:132: error: expected '}'
Bool = QMetaType::Bool,
^
This error, and a dozen similar ones, keeps appearing when I build. Its coming from the Qt Core.
Are there things that need to be rewritten when you move from a windows platform to osx using cimg? As far as I know the code should be portable..
I tried to reinstall libraries, install XQuarts, add library and include paths to the project file, but still the same problem :( Thanks in advance for your help!
What are you building? If you're building Qt, then you have some local modifications that broke it - start with fresh sources.
If you're building some code that uses Qt, then the problem is in that code, not in Qt. The smell of this issue is that something has defined a Bool macro that interferes with QVariant's code.
The file where this happens is not qvariant.h. It is some .cpp file of yours - look at the error messages, they will include something like "while compiling yourfile.cpp". In that file, you need to put #include <QVariant> as the very first line. This should fix the problem temporarily.
You should search for Bool in your code and fix the global namespace pollution. Your code is broken, no doubt about it I think.
The X11 library (used by default by CImg) defines Bool.
As you are working with Qt, you probably don't want to use display capabilities of CImg, so I suggest you deactivate it by defining the macro cimg_display=0when compiling.
CImg will not try to include the X11 headers, and it may solve your problem.

SuperLU library XCode issues

I'm a newbie when compiling and adding 3rd party C++ libraries into XCode... So here's the problem. I have .cpp files wich depend on 3 libraries. OpenCV-Eigen-SuperLU. So far think that I managed to add correctly OpenCV-Eigen, but I got trouble adding SuperLU.
I'm really confused on where should I include source files paths??. Some tutorials say that I need to create a static lib... other that I need a framework... on the other hand there are XCode project template which just include src files into a folder inside XCode project's and then reference them from build setting properties. Anyway I really need some guidance here.
So here's my setup for most of the libs. I have a folder within the XCode project with all dependencies.
Then... on project build settings I setup header search paths-- and library search paths. To point that folder
Headers for SuperLU are found correctly... but I get Linker errors.
So as you can see, I have no idea what to do... I'm Sorry but as I told on beginning I'm a newbie adding libs into XCode... plus documentation of procedures it's not good at all. So if anybody could help me with some guidance or links where I can start understanding how the linking process works would be great.
I'm a little desperate at this point, os any kind of help would be really really great.
Thanks a lot.
The problem is that both SuperLU and Objective-C define YES and NO. So you get a conflict.
In the end I was advised to create a different target to compile all SuperLU as a pure c++ project with gcc compiler. That made it possible for iOS app to use the lib as a dependency.

How do I zip a directory of files using C++?

I'm working on a project using C++, Boost, and Qt. I understand how to compress single files and bytestreams using, for example, the qCompress() function in Qt.
How do I zip a directory of multiple files, including subdirectories? I am looking for a cross-platform (Mac, Win, Linux) solution; I'd prefer not to fire off a bunch of new processes.
Is there a standard way to combine bytestreams from multiple files into a zipped archive, or maybe there is a convenience function or method that would be available in the Boost iostream library?
Many thanks for the assistance.
Update: The QuaZip library looks really great. There is an example in the download package (in the "tests" dir) that shows very clearly how to zip up a directory of files.
Update 2: After completing this task on my Linux build environment, I discovered that QuaZip doesn't work at all with the Visual Studio compiler. It may be possible to tackle all those compiler errors, but a word of caution to anyone looking down this path.
I have found the following two libraries:
ZipIOS++. Seems to be "pure" C++. They don't list Windows explicitly as a supported platform. So i think you should try your luck yourself.
QuaZIP. Based on Qt4. Actually looks nice. They list Windows explicitly (Using mingw). Apparently, it is a C++ wrapper for [this] library.
Ah, and of course, i have ripped those sites from this Qt Mailinglist question about Zipping/Unzipping of directories :)
Just for the record...
Today, I needed to do something very similar in Visual C++. (Though wanted to maintain the possibility to compile the project on other platforms; however preferred not to adopt Qt just for this purpose.)
Ended up using the Minizip library. It is written in plain C, but devising a simple C++ wrapper around it was a breeze, and the end result works great, at least for my purposes.
I have built a wrapper around minizip adding some features that I needed and making it nicer to use it. Is does use the latest c++11 and is developed using Visual Studio 2013, so it should work out-of-the-box for you.
There's a full description here: https://github.com/sebastiandev/zipper
you can zip entire folders, streams, vectors, etc. Also a nice feature is doing everything entirely in memory.
Poco::Zip is also a choice, it has clearly documentation and some code for demo.
Poco::Zip Document
system("zip myarchive.zip *");
I tried QuaZIP 0.4.3 on Windows with VisualStudio 2010 -- there are still issues but can be resolved easily.
To build with VS:
Use CMake to configure and generate VS solution for QuaZIP.
Open soltion with VS and build -- you'll first notice that it can't find 'zlib.h'.
Open preferences for quazip project and add path to Qt's copy of Zlib to C/C++->General->Additional Include Directories: $(QTDIR)/src/3rdparty/zlib.
Rebuild again and you'll get lots of warnings and one error C2491: dllimport static issue on QuaZipFile::staticMetaObject.
This is because QuaZipFile is declared like "class QUAZIP_EXPORT QuaZipFile" and QUAZIP_EXPORT needs to resolve to Q_DECL_EXPORT for dll and to Q_DECL_IMPORT for application, based on whether QUAZIP_BUILD is defined or not. When building QuaZIP QUAZIP_BUILD should be defined but isn't -- configuration process defines in error completely useless "quazip_EXPORTS" instead.
To fix, just remove "quazip_EXPORTS" from all build configurations and add QUAZIP_BUILD instead -- QuaZIP will now build fine.