In my project I need to create QObject derived classes (with Q_OBJECT and signals) using a macro. The macro resides in a separate file. Here is a simplified example:
The macro is declared in the file CreateQtClass.h:
#define CREATE_QT_CLASS( ClassName ) \
class ClassName : public QObject \
Q_SIGNAL void mySignal( void ); \
I use the macro to create my class in the file MyQtClass.h
CREATE_QT_CLASS( MyQtClass );
In my .qbs file, I add MyQtClass.h to the files property, like this:
files: [ "main.cpp", "MyQtClass.h" ]
The file main.cpp:
static void mySlot( void )
qDebug() << "Hello slot";
int main( void )
QObject::connect( &c, &MyQtClass::mySignal, &mySlot );
qbs doesn't run 'moc' on MyQtClass.h. It looks like it doesn't do the scanning correctly, and doesn't detect the Q_OBJECT inside the macro.
I have created a simple Makefile to shows that when manually forcing moc to run on MyQtClass.h, everything works well. This is the Makefile:
CXX = /usr/bin/g++
QT_VERSION = 5.11.2
QT_PATH = /home/user/programs/Qt$(QT_VERSION)/$(QT_VERSION)/gcc_64
MOC = $(QT_PATH)/bin/moc
INCLUDES = \
LINK_FLAGS = \
-m64 $(QT_PATH)/lib/libQt5Core.so.$(QT_VERSION) \
C_FLAGS = \
SOURCES = main.cpp
OBJS = $(SOURCES:%.cpp=%.cpp.o)
HEADERS_THAT_NEED_MOC = MyQtClass.h
MOC_OBJS = $(HEADERS_THAT_NEED_MOC:%.h=moc_%.cpp.o)
HelloWorld-Qt: $(OBJS) $(MOC_OBJS)
$(CXX) $^ $(LINK_FLAGS) -o $@
%.cpp.o : %.cpp
$(CXX) $(C_FLAGS) -c $^ -o $@
$(MOC) -DQT_CORE_LIB $(INCLUDES) $^ -o $@
rm -f *.cpp.o HelloWorld-Qt moc_*.cpp
I think that the problem is that qbs and moc do not scan the files in the same manner. Qbs tries to detect whether the file MyQtClass.h needs to be moc'ed but fails where moc itself succeeds.
The lack of consistency between the two tools (qbs, moc) can lead to situations where files are not detected correctly.
For this reason I think that we need some kind of way to manually tell Qbs that a file (MyQtClass.h, in my case) needs to be moc'ed.
What I suggest is that we add new a fileTag such as "force_moc" which we can attached to files such as MyQtClass.h and force qbs to run moc on them.
Attached all the files to reproduce the problem + the Makefile