How enable deprecated functions in Qt5 - c++

I want to port a Qt4 program to Qt5 and some functions are not defined (such as QHeaderView::setMoveable), but I see in the qheaderview.h file that with some magic defines (QT_DEPRECATED_SINCE) it should be possible to reenable them.
What do I have to do in order to let QHeaderView::setMovable reappear? I do not want to rewrite my code if there is a way like that.

You can add to your .pro file the following line:
DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0

Related

Is it possible to create own macros in C++ to be addressed later by QMake?

I'm new to C++ and Qt and want to know about the macros which can be used by QMake. in QMake DEFINES += xy_macro adds xy_macro to the *.pro file. Is it possible to define my own macro and where should I save it to use it later by QMake? The issue is that I want to define the SOURCES which must be included in the Makefile. The list of the source file must be then automatically passed to the *.pro file.

Compiling QT code with other compilers

I am compiling scientific code that performs numerically intensive calculations. My process is the following:
Code targeted for the CPU is compiled by the Intel C++ Compiler
Code targeted for the GPU is compiled by NVCC
The object files are then linked using the Intel C++ Compiler
To do this, I have written a makefile to perform the necessary steps, and everything is carried out on the command line. Now, I wish to add on a GUI to the program, using Qt, but without using Qt Creator. As a test, I am trying to compile the "Hello World! Desktop application" given here: https://wiki.qt.io/Getting_Started_on_the_Commandline
My interpretation is as follows:
#include <QtGui\QtGui>
#include <QtWidgets\QApplication>
#include <QtWidgets\QLabel>
void test_qt()
{
QApplication app();
QLabel label("Hello, world!");
label.show();
app.exec();
}
I call the function in a main.cpp file. In my makefile, I link with Qt5core.lib, Qt5Gui.lib and Qt5Widgets.lib, and as per the makefile rules, this is compiled with the Intel C++ compiler. However, it gives the following error:
error: expression must have class type
app.exec();
^
My question is as follows:
How can I edit my makefile to compile Qt code? I will be needing signals and slots, so moc may be needed according to Can I use Qt without qmake or Qt Creator? but I will not be using uic.
Update based on the discussion below:
Looking at the QApplication constructor reference, it would appear that it is initialized with the command line arguments argc and argv obtained in main(). This makes it possible to pass Qt-specific flags to the Qt infrastructure. Thus, it cannot be created with no arguments as in your example -- you need to pass argc and argc from main.
As for compiling the files containing signals and slots - these features are not standard C++, so they need to be preprocessed by a tool that knows what they mean. If I understand correctly, moc converts these Qt-specific features into standard C++ code which must then be compiled using your compiler. So:
Use a naming convention for your Qt-specific cpp files so you can create a makefile pattern to process them with moc. ex: file.moc.cpp
Create a dependency on file.cpp in your makefile, which depends on file.moc.cpp
Create a rule for creating .cpp files from .moc.cpp files and invoke moc in that rule.
Call your normal compiler on the .cpp files. Don't forget your include directories, etc.
Make sure you do not check in these generated files, as they will change each time your .moc.cpp file changes. Maybe dump them in a temp directory that is ignored by your revision control? You may want to search around to see how other sample projects do it.
Side Note: declaring app without the parentheses could lead to a case of The Most Vexing Parse.
https://en.wikipedia.org/wiki/Most_vexing_parse
When an object is declared, but the parentheses are empty, the compiler ends up interpreting the line as a a function prototype for a function that takes no arguments and returns a QApplication.

Custom compilation flag to enable specific feature

I have a Qt project which requires a library (gphoto2) to enable some features that are not essential. I'd like to add some sort of configuration option to my qmake or make call to enable features using this library, so I can compile without it being installed.
What is the best way to configure something like this?
I guess I need some way to add a define based on a compiler parameter, which I can query in my code using #ifdef ...
I assume you use make (without qmake). It is reasonable and quite easy to use GNU make (alone) on Qt projects. You could use some other build automation tool like ninja.
Then you could decide to enable that Gphoto feature by compiling your code with -DWITH_GPHOTO and using #if WITH_GPHOTO in your C++ code.
You would compile by adding
CXXFLAGS+= -DWITH_GPHOTO
in your Makefile
I won't call that a "custom compiler flag" (e.g. like GCC plugins can provide) but a preprocessor flag. It is pretty standard practice.
Maybe you also want to pass such flags to moc. Then your Makefile is running moc thru some rule and command, which you could tailor too.
BTW, you might consider GNU autoconf or some other Makefile generator like cmake. I don't think you should spend too much time on these...
PS. I don't know how that idea translates into qmake and leave you to find out.
Assuming, you are using qmake, you can add a preprocessor definition depending on the existence of a file or an environment variable.
You could add a qmake project for compiling your external library and place it relative to your own project by default.
LIBGPHOTO2_PATH = $$getenv(LIBGPHOTO2PATH)
isEmpty(LIBGPHOTO2_PATH): LIBGPHOTO2_PATH = ../../libgphoto2
exists($$LIBGPHOTO2_PATH/libgphoto2.pri): include($$LIBGPHOTO2_PATH/libgphoto2.pri)
In libgphoto2.pri you add a preprocessor definition to indicate the presence of libgphoto2, add include and linker paths etc.:
DEFINES += WITH_LIBGPHOTO2
In the code of your dependent project, you check for the presence using #ifdef.
Instead of creating a qmake-project to compile, you could also check for the presence of the compiled library at a given path and set values directly (I don't know how libgphoto compiles, so I assume a default directory structure with include/, lib/ etc):
LIBGPHOTO2_PATH=$$getenv(LIBGPHOTO2PATH)
isEmpty(LIBGPHOTO2_PATH): LIBGPHOTO2_PATH = ../../libgphoto2
exists($$LIBGPHOTO2_PATH/include) {
DEFINES += WITH_LIBGPHOTO2
INCLUDEPATH += $$LIBGPHOTO2_PATH/include
LIBS += -L$$LIBGPHOTO2_PATH/lib -lgphoto2
}
You should however consider to move to something more modern like qbs, which is a lot faster, more flexible and easier to read.

Qt macro keywords cause name collisions

I am building an NCurses interface for my Qt project. I want to use CDK but I think the signals member of this struct is colliding with the Qt signals keyword.
/usr/include/linux/cdk.h:411: error: expected unqualified-id before 'protected'
How can I get CDK to work with Qt?
You can define the QT_NO_KEYWORDS macro, that disables the “signals” and “slots” macros.
If you use QMake:
CONFIG += no_keywords
(Qt Documentation here)
If you’re using another build system, do whatever it needs to pass -DQT_NO_KEYWORDS to the compiler.
Defining QT_NO_KEYWORDS will require you to change occurrences of signals to Q_SIGNALS and slots to Q_SLOTS in your Qt code.
If you cannot change all the Qt code, e.g. because you're using third-party libraries not being "keyword-clean", you could try to undefine "signals" locally before including cdk.h:
#undef signals
#include <cdk.h>
I'd recommend to use no_keywords though if possible, as it is less tedious and error-prone.

unable to call c++ functions in qt

I have a shared library of the C++ application and I am able to call it using the test app. I want to write a Qt UI for this. I am not able to call the C++ functions directly. Only if I give name mangled function name it works.
Also if I create an object of the C++ class and call a function of the class I am getting "undefined reference to " the function.
How can I call the C++ functions and create objects of C++ class and call functions on them?
You must include the path to the headers files. To do this in Qt Creator, modify your .pro file to include the following line:
INCLUDEPATH += path/to/header/files
you will notice that you MUST use the slash above... If you try using '\', it will not work.
It sounds like you're not correctly including the header files for the code you want to use. The "extra" features of QT don't stop all of the normal C++ features from working.