13

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
frarugi87
  • 2,826
  • 1
  • 20
  • 41
  • This now seems to be a QMake question about defining preprocessor symbols. Should we add the qmake tag? – drescherjm Sep 11 '14 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 Sep 11 '14 at 14:29

2 Answers2

16

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!

frarugi87
  • 2,826
  • 1
  • 20
  • 41
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.

kiss-o-matic
  • 1,111
  • 16
  • 32
  • 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 Jul 04 '16 at 09: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 May 20 '20 at 07: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](https://stackoverflow.com/questions/4397478)). 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 May 20 '20 at 11:09