14

I have just one question: is a define (or something similar) available if you are compiling some code to know if the GUI flag has been set?

I'll explain better. I have some code I want to reuse for different programs in QT. Now, some parts of this code are used just in GUI applications (or better widget applications) and depend on QtGui and QtWidgets. I'd like to put these parts in a conditional block (#if or #ifdef) for them to be compiled only in the projects where the GUI and/or widget libraries are included.

And, before you suggest this, making a library is not a solution. I'd prefer a define...

EDIT:
Probably I didn't explain myself clearly. What I'm looking for is the define associated with the GUI inclusion. Example:

FILE myfile.h

#ifdef THE_QT_GUI_DEFINE_FLAG
#include <QPainter.h>
#endif

PROJECT A: in the QMake file I write:

QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

PROJECT B: in the QMake file I write:

QT       -= gui

Now, I want QPainter.h to be included just in project A. Do you know what is the define flag set when I add the gui library? I tried with QT_QTGUI_MODULE_H, but it doesn't work (probably because it is used just when you compile THE qt library).

2
  • This now seems to be a QMake question about defining preprocessor symbols. Should we add the qmake tag?
    – drescherjm
    Commented Sep 11, 2014 at 14:27
  • Ehm... No, it's not a question about telling QMake how to define a preprocessor symbol. It's a "what preprocessor symbol is defined when, through QMake, i add the gui feature?" question
    – frarugi87
    Commented Sep 11, 2014 at 14:29

2 Answers 2

17

Ok, I found it. Inspired by the answers I went to dig in the automatically generated files and, searching through the lib files, i found

Qt\5.2.1\msvc2010\mkspecs\modules\qt_lib_gui.pri

which has the line

QT.gui.DEFINES = QT_GUI_LIB

and then... This is the magic word! :)

Now if i put

#ifdef QT_GUI_LIB
#include <QPainter.h>
#endif

QPainter is included just in the gui-enabled projects.

Thank you all for your help!

2
  • Always nice to know this.
    – Niall
    Commented Sep 11, 2014 at 14:40
  • 5
    and if you just need the core lib it is 'QT_CORE_LIB'
    – Zaiborg
    Commented Sep 11, 2014 at 14:40
0

When in doubt about anything in Qt, remember this:

Qt is not "like C++". It is C++. If it's legal in C++, then it's legal in Qt. Some special rules come in with QObject (multiple inheritance for example) but if it's something from the C++ side, it's almost always going to work.

3
  • 1
    I disagree. Qt is not like C++, it doesn't obey all the rules of C++ (like templates) and adds rules of its own (moc, signals and slots). Furthermore the ownership model in Qt is unlike anything else in C++. They even added their own type-traits system. If you wish to keep your sanity accept that Qt is a dialect of C++. Knowing how C++ does things is a good start, but it is sometimes not sufficient to figure out Qt.
    – nwp
    Commented Jul 4, 2016 at 9:07
  • @nwp Qt is built on C++. A framework does not have to use all features of the language (like templates). In the same time, signals and slots are written using C++ code, MOC generates C++ code. As a result, you can use any standard-following C++ compiler to compile a Qt application. If it were not C++, you’d have to use an adapted compiler.
    – Melebius
    Commented May 20, 2020 at 7:26
  • 1
    @Melebius Qt does use templates, but that's not what I meant. What I meant is that Qt prohibits you from using templates because moc does not understand them (see this). It doesn't even understand typedefs because signal/slot finding is based on string equality and not on type equality. Those are massive enough limitations that I feel that saying "Qt-C++ is a dialect of real C++" is more accurate than "Qt-C++ is regular C++", but it depends on word definitions and point of view. I don't think there is a real disagreement here.
    – nwp
    Commented May 20, 2020 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.