Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using Qt: Defined C++ symbol
Forum Update on Monday, May 27th 2025

Using Qt: Defined C++ symbol

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fem_dev
    wrote on 18 Jul 2020, 23:21 last edited by
    #1

    I'm developing a C++ shared library using Qt and CMake.
    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 the Q_OBJECT macro and the signals 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 and Qt 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 and Qt 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,

    K W 2 Replies Last reply 19 Jul 2020, 03:23
    0
    • F fem_dev
      18 Jul 2020, 23:21

      I'm developing a C++ shared library using Qt and CMake.
      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 the Q_OBJECT macro and the signals 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 and Qt 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 and Qt 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,

      W Offline
      W Offline
      wrosecrans
      wrote on 19 Jul 2020, 03:39 last edited by
      #3

      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.

      F 1 Reply Last reply 19 Jul 2020, 14:45
      3
      • F fem_dev
        18 Jul 2020, 23:21

        I'm developing a C++ shared library using Qt and CMake.
        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 the Q_OBJECT macro and the signals 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 and Qt 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 and Qt 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,

        K Offline
        K Offline
        kshegunov
        Moderators
        wrote on 19 Jul 2020, 03:23 last edited by kshegunov
        #2

        What I'd do is test this during the make generation step (qmake code, you need to check yourself how to do it with cmake as I'm not familiar with what's available there):

        CONFIG(qt): DEFINES += USING_QT  # We are building with/against Qt
        

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        1
        • F fem_dev
          18 Jul 2020, 23:21

          I'm developing a C++ shared library using Qt and CMake.
          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 the Q_OBJECT macro and the signals 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 and Qt 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 and Qt 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,

          W Offline
          W Offline
          wrosecrans
          wrote on 19 Jul 2020, 03:39 last edited by
          #3

          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.

          F 1 Reply Last reply 19 Jul 2020, 14:45
          3
          • W wrosecrans
            19 Jul 2020, 03:39

            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.

            F Offline
            F Offline
            fem_dev
            wrote on 19 Jul 2020, 14:45 last edited by
            #4

            @wrosecrans thank you!

            1 Reply Last reply
            0
            • F Offline
              F Offline
              fem_dev
              wrote on 21 Jul 2020, 13:46 last edited by
              #5

              @kshegunov said in Using Qt: Defined C++ symbol:

              how to do it with cmake

              add_definitions(-DUSING_QT)
              
              K 1 Reply Last reply 21 Jul 2020, 18:05
              0
              • F fem_dev
                21 Jul 2020, 13:46

                @kshegunov said in Using Qt: Defined C++ symbol:

                how to do it with cmake

                add_definitions(-DUSING_QT)
                
                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 21 Jul 2020, 18:05 last edited by
                #6

                @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.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                0

                1/6

                18 Jul 2020, 23:21

                • Login

                • Login or register to search.
                1 out of 6
                • First post
                  1/6
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved