Using Qt: Defined C++ symbol
-
wrote on 18 Jul 2020, 23:21 last edited by
I'm developing a C++
shared library
using Qt andCMake
.
I'm trying to write a single source-code that be possible to compile in machines with or without installed Qt environment.To do that, I'm using a custom
#ifdef USING_QT
symbol to enable/disable blocks of code depending of the compile system.
Example: with that, I can disable theQ_OBJECT
macro and thesignals and slots
methods.#ifdef USING_QT // Code that used Qt features #else // The same code when Qt environment is not available #endif
Now, I would like to replace this custom C++ symbol to a official Qt symbol that always will be present if the developer try to compile this project using Qt.
I was using the
#ifdef QT_VERSION
in the past (Qt 5.11
andQt Creator IDE 4.10.0
), but in this new project, when I try to use this Qt macro, the Qt Creator IDE mark the internal code as disabled, like if this symbol was not defined.Today, I'm using
Qt 5.15.0
andQt Creator 4.12.4
on Ubuntu 20.04 x64.#ifdef QT_VERSION x = 10; // QT CREATOR PUTS GREY COLOR (Disabled) #endif
Is there a better official Qt symbol to choice in this case?
May be, there was any changes in Qt Libraries or Qt Creator IDE that changed the text editor color highlight to disable blocks of code inside the
#ifdef QT_VERSION
macro? I don't know...Obs.: I searched some macros in
QtGlobal
https://doc.qt.io/qt-5/qtglobal.html but I couldn't found a good option.Thank you,
-
I'm developing a C++
shared library
using Qt andCMake
.
I'm trying to write a single source-code that be possible to compile in machines with or without installed Qt environment.To do that, I'm using a custom
#ifdef USING_QT
symbol to enable/disable blocks of code depending of the compile system.
Example: with that, I can disable theQ_OBJECT
macro and thesignals and slots
methods.#ifdef USING_QT // Code that used Qt features #else // The same code when Qt environment is not available #endif
Now, I would like to replace this custom C++ symbol to a official Qt symbol that always will be present if the developer try to compile this project using Qt.
I was using the
#ifdef QT_VERSION
in the past (Qt 5.11
andQt Creator IDE 4.10.0
), but in this new project, when I try to use this Qt macro, the Qt Creator IDE mark the internal code as disabled, like if this symbol was not defined.Today, I'm using
Qt 5.15.0
andQt Creator 4.12.4
on Ubuntu 20.04 x64.#ifdef QT_VERSION x = 10; // QT CREATOR PUTS GREY COLOR (Disabled) #endif
Is there a better official Qt symbol to choice in this case?
May be, there was any changes in Qt Libraries or Qt Creator IDE that changed the text editor color highlight to disable blocks of code inside the
#ifdef QT_VERSION
macro? I don't know...Obs.: I searched some macros in
QtGlobal
https://doc.qt.io/qt-5/qtglobal.html but I couldn't found a good option.Thank you,
wrote on 19 Jul 2020, 03:39 last edited byIn Cmake, you could do something like
find_package(Qt5 COMPONENTS Core Gui Widgets) if (Qt5_FOUND) target_compile_definitions(MyCoolLibrary PUBLIC MYCOOLLIBRARY_USE_QT=1) endif()
If you don't include any Qt headers, there won't be anything like QT_VERSION defined. The definitions for that stuff is in those Qt headers, so you can't use it to decide whether or not to include them. You need to use your own flag in the configuration to make the decision.
-
I'm developing a C++
shared library
using Qt andCMake
.
I'm trying to write a single source-code that be possible to compile in machines with or without installed Qt environment.To do that, I'm using a custom
#ifdef USING_QT
symbol to enable/disable blocks of code depending of the compile system.
Example: with that, I can disable theQ_OBJECT
macro and thesignals and slots
methods.#ifdef USING_QT // Code that used Qt features #else // The same code when Qt environment is not available #endif
Now, I would like to replace this custom C++ symbol to a official Qt symbol that always will be present if the developer try to compile this project using Qt.
I was using the
#ifdef QT_VERSION
in the past (Qt 5.11
andQt Creator IDE 4.10.0
), but in this new project, when I try to use this Qt macro, the Qt Creator IDE mark the internal code as disabled, like if this symbol was not defined.Today, I'm using
Qt 5.15.0
andQt Creator 4.12.4
on Ubuntu 20.04 x64.#ifdef QT_VERSION x = 10; // QT CREATOR PUTS GREY COLOR (Disabled) #endif
Is there a better official Qt symbol to choice in this case?
May be, there was any changes in Qt Libraries or Qt Creator IDE that changed the text editor color highlight to disable blocks of code inside the
#ifdef QT_VERSION
macro? I don't know...Obs.: I searched some macros in
QtGlobal
https://doc.qt.io/qt-5/qtglobal.html but I couldn't found a good option.Thank you,
What I'd do is test this during the make generation step (
qmake
code, you need to check yourself how to do it withcmake
as I'm not familiar with what's available there):CONFIG(qt): DEFINES += USING_QT # We are building with/against Qt
-
I'm developing a C++
shared library
using Qt andCMake
.
I'm trying to write a single source-code that be possible to compile in machines with or without installed Qt environment.To do that, I'm using a custom
#ifdef USING_QT
symbol to enable/disable blocks of code depending of the compile system.
Example: with that, I can disable theQ_OBJECT
macro and thesignals and slots
methods.#ifdef USING_QT // Code that used Qt features #else // The same code when Qt environment is not available #endif
Now, I would like to replace this custom C++ symbol to a official Qt symbol that always will be present if the developer try to compile this project using Qt.
I was using the
#ifdef QT_VERSION
in the past (Qt 5.11
andQt Creator IDE 4.10.0
), but in this new project, when I try to use this Qt macro, the Qt Creator IDE mark the internal code as disabled, like if this symbol was not defined.Today, I'm using
Qt 5.15.0
andQt Creator 4.12.4
on Ubuntu 20.04 x64.#ifdef QT_VERSION x = 10; // QT CREATOR PUTS GREY COLOR (Disabled) #endif
Is there a better official Qt symbol to choice in this case?
May be, there was any changes in Qt Libraries or Qt Creator IDE that changed the text editor color highlight to disable blocks of code inside the
#ifdef QT_VERSION
macro? I don't know...Obs.: I searched some macros in
QtGlobal
https://doc.qt.io/qt-5/qtglobal.html but I couldn't found a good option.Thank you,
wrote on 19 Jul 2020, 03:39 last edited byIn Cmake, you could do something like
find_package(Qt5 COMPONENTS Core Gui Widgets) if (Qt5_FOUND) target_compile_definitions(MyCoolLibrary PUBLIC MYCOOLLIBRARY_USE_QT=1) endif()
If you don't include any Qt headers, there won't be anything like QT_VERSION defined. The definitions for that stuff is in those Qt headers, so you can't use it to decide whether or not to include them. You need to use your own flag in the configuration to make the decision.
-
In Cmake, you could do something like
find_package(Qt5 COMPONENTS Core Gui Widgets) if (Qt5_FOUND) target_compile_definitions(MyCoolLibrary PUBLIC MYCOOLLIBRARY_USE_QT=1) endif()
If you don't include any Qt headers, there won't be anything like QT_VERSION defined. The definitions for that stuff is in those Qt headers, so you can't use it to decide whether or not to include them. You need to use your own flag in the configuration to make the decision.
wrote on 19 Jul 2020, 14:45 last edited by@wrosecrans thank you!
-
wrote on 21 Jul 2020, 13:46 last edited by
@kshegunov said in Using Qt: Defined C++ symbol:
how to do it with cmake
add_definitions(-DUSING_QT)
-
@kshegunov said in Using Qt: Defined C++ symbol:
how to do it with cmake
add_definitions(-DUSING_QT)
@fem_dev said in Using Qt: Defined C++ symbol:
add_definitions(-DUSING_QT)
The point was that the distinction could, and I'd argue it should, be made automatically, which as far as I can tell is @wrosecrans' solution.
1/6