Author Topic: Qt and moc with code blocks  (Read 9035 times)

Offline mariusc

  • Single posting newcomer
  • *
  • Posts: 2
Qt and moc with code blocks
« on: September 28, 2009, 06:01:59 pm »
Hi,
Codebloks: Welcome to Code::Blocks 8.02!
OS: Ubuntu 9.0.2 jaunty

I have problems compiling a generated then expanded (uses slots) Qt application with moc-qt4 utility.
I was following the steps from http://forums.codeblocks.org/index.php?topic=615.0 but I guess is pretty old because
the procedure from above link.

1 - Go to QT app properties and check the "this is a custom makefile" checkbox. requires that Makefile should exist (but I have no makefile because I've been using cb )

The prerequisites  are:
 - no make file. all Files are part of a cbp project
 - The procedure as in above link
 
Code
1 - Go to QT app properties and check the "this is a custom makefile" checkbox.
     Now click on project's build options button, select the default brach at left and, in commands area, type the following into pre-build-steps:
     qmake -project
     qmake
2 - Make sure you have selected "invoke compiler directly" as the build method in settings->compiler->other.
3 - Compile the project; if you have includes of type "#include <QtGui>" those won't be found, returning errors. The makefile is now created.
4 - Change again the build method, now it must be "Work with makefiles" and try to compile again; it must work now.

yield when building:

/bin/sh: -f: not found
Process terminated with status 127 (0 minutes, 0 seconds)
0 errors, 0 warnings

Then I tried to run moc-qt4 separate and generate moc_*.cpp. for 'Q_OBJECT' @ headers. I've added the fiels to project and
now I get
 
error: #error "This file was generated using the moc from 3.3.8b. It"
error: #error "cannot be used with the include files from this version of Qt."
error: #error "(The moc has changed too much.)"


then

/usr/include/qt3/private/qucom_p.h:75: error: function definition does not declare parameters
/usr/include/qt3/private/qucom_p.h:83: error: function definition does not declare parameters


...

Thank you







Offline mariusc

  • Single posting newcomer
  • *
  • Posts: 2
Re: Qt and moc with code blocks Fixed
« Reply #1 on: September 28, 2009, 09:23:35 pm »
Hi,
I fix it in several steps. It might be useful for anyone ran into it...
I made a script in /usr/bin qt-mocit.sh (I fond it somewhere)

Code
#!/bin/sh 
cd `pwd`
echo `pwd`
mkdir moc # create s sub folder for current src files
rm -f "moc/moc_includes.h"
grep -l Q_OBJECT *.h > files_to_moc
cat files_to_moc | while read line
do
ext=h  
output=${line%h}  
moc-qt4 $line -o "moc/moc_$output""cpp"
echo "include ""\"moc_$output""cpp\"" >> moc/moc_includes.h
done
Run this from current working folder where src files are
 - Add all /moc/*.cpp files to the project
 - define for the current build target in Build Obtions::CompilerSettings::#defines
  
Code
Q_MOC_OUTPUT_REVISION=61






Offline rickg22

  • Lives here!
  • ****
  • Posts: 2283
Re: Qt and moc with code blocks
« Reply #2 on: November 07, 2010, 06:14:30 pm »
Hi all! Long time, no see.

Anyway, I found a way to use moc files without having to use makefiles. Found this somewhere on the web:

Create some "filename.moc_trigger" (the actual name doesn't matter). Right click on the file and select "properties". Under "build", click on "compile", and set the priority to zero (so that it gets "compiled" before any other files).

Under "advanced", click on "Use custom command to build this file".

Then type:

Code
moc-qt4 your_c_or_h_file -o your_moc_file

And #include your_moc_file in your_c_or_h_file.

Also, if you want to play it safe, you can use:

Code
#ifndef Q_MOC_RUN
  #include "your_moc_file"
#endif

so that moc doesn't try to parse the generated file itself.

@mariusc: If you need to define Q_MOC_OUTPUT_REVISION=61, then you're not including some needed Qt headers. You need to check your code again.
« Last Edit: November 07, 2010, 06:17:04 pm by rickg22 »

Offline Slammer

  • Multiple posting newcomer
  • *
  • Posts: 15
Re: Qt and moc with code blocks
« Reply #3 on: January 04, 2011, 01:06:53 pm »
This method works very nice...

To make things cleaner I put moc generated files in a separate directory using the following in the custom command:

moc -o $file_dir/moc/$file_name.cpp $file

The generated file has cpp extension and you can include it in the project or #include it inside the file.