Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 4

Qt-interest Archive, May 2007
deeper MOC documentation/information and Qt's preprocessing


Message 1 in thread

hello everyone.

from my understanding, moc only preprocesses header (*.h) files,
correct? .. so for example, the following code:

class Main : public QObject
{
  Q_OBJECT
public:
    Main();
    void methodA();
    void methodB( int a );
  signals:
     void signalA();
  public slots:
    void slotA();
};

will result in a "moc_main.cpp" being created.. since stuff such as
"public signals", "signals" etc are not standard C++, moc creates
appropriate code...

what happens in the case of inside the implementation file "main.cpp"
when it states "emit signalA();"  .. now the "emit" is no c++ thing,
but the "signalA()" is just a method invocation (i see how it goes to
qt_metacall in "moc_main.cpp" and is called in there...)

but moc does not do anything with the "main.cpp" file does it? ... so
what happens to the "emit" keyword?

the reason i am asking, is because i would like to use moc to do some
preprocessing in the implementation file... is this possible? .. or
does moc only work on header files?

... if i have not bored you yet, here's further explanation on what i
would like to achieve:

below, i would like to create my own keyword called "Q_SILLY", and
annotation a method definition with this new keyword...

class Main : public QObject
{
  Q_OBJECT
public:
    Main();
    void methodA();
    Q_SILLY void methodB( int a );
  signals:
     void signalA();
  public slots:
    void slotA();
};

in my "main.cpp" file i would have code such as this:

void Main::methodA()
{
    // do some stuff...

   methodB( 123 );

   // do other stuff...
}

note that methodB() was annotated as "Q_SILLY".. so i would actually
like the preprocessor to pick this up, and instead of doing a normal
method invocation, to produce other code... for arguments sake, i
would like it to do something like:


void Main::methodA()
{
    // do some stuff...

   cout<< "methodB" << endl;

   // do other stuff...
}

this cannot be done with ordinary preprocessor macros, since need a
preprocessor to figure out "methodB" was annotated as Q_SILLY

does that make sense?.. i want the preprocessor to understand that
"methodB" is no ordinary method, and that it should do something
else...

i'm trying to understand moc, and see how it handles keywords such as
"signals" etc.. i thought i could find answers in
INSTALL_DIR/src/tools/moc  but i can't see where the "signals" token
was defined, etc...

i would appreciate any sort of guidance for the direction to search
in... or if there are documentations available for moc.. but i'm
guessing that sort of information would be kept private to Trolltech
:-p  ..

thank you very much for your time.

cheers,
nasser

--
 [ signature omitted ] 

Message 2 in thread

On 17.05.07 17:59:29, Nasser wrote:
> >from my understanding, moc only preprocesses header (*.h) files,
> correct? .. so for example, the following code:

Well, it does preprocess files that have C++ header content (they don't
need to be called foo.h)

> what happens in the case of inside the implementation file "main.cpp"
> when it states "emit signalA();"  .. now the "emit" is no c++ thing,
> but the "signalA()" is just a method invocation (i see how it goes to
> qt_metacall in "moc_main.cpp" and is called in there...)

emit is just a macro that is defined to do nothing, thus emit signalA();
is turned into a simple signalA(); function call. signalA(); otoh is
implemented in the generated code from moc, which then calls all
connected slots.

> the reason i am asking, is because i would like to use moc to do some
> preprocessing in the implementation file... is this possible? .. or
> does moc only work on header files?

I wish you much luck with this, moc's source seems to be generated
partly from some grammar, as I doubt a human reads and writes that
parser code.

> below, i would like to create my own keyword called "Q_SILLY", and
> annotation a method definition with this new keyword...
> 
> does that make sense?.. i want the preprocessor to understand that
> "methodB" is no ordinary method, and that it should do something
> else...

Well, instead of Q_SILLY in the header your simple example could also
just use

Q_SILLY(methodB) 

in the implementation and Q_SILLY can then evaluate to the cout <<
thing. But I guess your ultimate goal doesn't allow for that.

> i'm trying to understand moc, and see how it handles keywords such as
> "signals" etc.. i thought i could find answers in
> INSTALL_DIR/src/tools/moc  but i can't see where the "signals" token
> was defined, etc...

Well, it has a parser in there, and the two signals token (signals and
Q_SIGNALS) are defined in the token.h. However the matching from a
string to the token is done based on the keywords table in keywords.cpp.
I don't really know how that is structured, so good luck on trying to
find that out :)

IMHO you're better off writing your own preprocessor, or asking TT to
give you the moc parser in a human readable form (as I said I doubt
somebody really works on the code that you get with Qt, I think they do
have some grammar for a parser and a lexer internally at TT). Of course
I may be wrong, and those guys at TT are really that crazy to work
directly with these files...

> i would appreciate any sort of guidance for the direction to search
> in... or if there are documentations available for moc.. but i'm
> guessing that sort of information would be kept private to Trolltech
> :-p  ..

No, its all there in the source, you just have to understand it :P

Andreas

-- 
 [ signature omitted ] 

Message 3 in thread

Thank you all for your replies and suggestions

i agree that it is next to impossible trying to work with
autogenerated code since i am human :-)

i will see over the next few days what to do.. if i cannot get
something human readable, then i might have to go to the option of
creating my own preprocessor.. which would be the last resort since
moc is a preprocessor anyway.. and since my aim is to add features to
Qt i will investigate this more..

thanks again!
nasser


On 5/17/07, Andreas Pakulat <apaku@xxxxxx> wrote:
> On 17.05.07 17:59:29, Nasser wrote:
> > >from my understanding, moc only preprocesses header (*.h) files,
> > correct? .. so for example, the following code:
>
> Well, it does preprocess files that have C++ header content (they don't
> need to be called foo.h)
>
> > what happens in the case of inside the implementation file "main.cpp"
> > when it states "emit signalA();"  .. now the "emit" is no c++ thing,
> > but the "signalA()" is just a method invocation (i see how it goes to
> > qt_metacall in "moc_main.cpp" and is called in there...)
>
> emit is just a macro that is defined to do nothing, thus emit signalA();
> is turned into a simple signalA(); function call. signalA(); otoh is
> implemented in the generated code from moc, which then calls all
> connected slots.
>
> > the reason i am asking, is because i would like to use moc to do some
> > preprocessing in the implementation file... is this possible? .. or
> > does moc only work on header files?
>
> I wish you much luck with this, moc's source seems to be generated
> partly from some grammar, as I doubt a human reads and writes that
> parser code.
>
> > below, i would like to create my own keyword called "Q_SILLY", and
> > annotation a method definition with this new keyword...
> >
> > does that make sense?.. i want the preprocessor to understand that
> > "methodB" is no ordinary method, and that it should do something
> > else...
>
> Well, instead of Q_SILLY in the header your simple example could also
> just use
>
> Q_SILLY(methodB)
>
> in the implementation and Q_SILLY can then evaluate to the cout <<
> thing. But I guess your ultimate goal doesn't allow for that.
>
> > i'm trying to understand moc, and see how it handles keywords such as
> > "signals" etc.. i thought i could find answers in
> > INSTALL_DIR/src/tools/moc  but i can't see where the "signals" token
> > was defined, etc...
>
> Well, it has a parser in there, and the two signals token (signals and
> Q_SIGNALS) are defined in the token.h. However the matching from a
> string to the token is done based on the keywords table in keywords.cpp.
> I don't really know how that is structured, so good luck on trying to
> find that out :)
>
> IMHO you're better off writing your own preprocessor, or asking TT to
> give you the moc parser in a human readable form (as I said I doubt
> somebody really works on the code that you get with Qt, I think they do
> have some grammar for a parser and a lexer internally at TT). Of course
> I may be wrong, and those guys at TT are really that crazy to work
> directly with these files...
>
> > i would appreciate any sort of guidance for the direction to search
> > in... or if there are documentations available for moc.. but i'm
> > guessing that sort of information would be kept private to Trolltech
> > :-p  ..
>
> No, its all there in the source, you just have to understand it :P
>
> Andreas
>
> --
> Your lucky color has faded.
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>

--
 [ signature omitted ] 

Message 4 in thread

Hi,

> but moc does not do anything with the "main.cpp" file does it? ... so
> what happens to the "emit" keyword?

The emit keyword always expands to... nothing. It's just "syntactic sugar".

 From src/corelib/kernel/qobjectdefs.:
	# define emit

--
 [ signature omitted ] 

Message 5 in thread

Nasser wrote:
> the reason i am asking, is because i would like to use moc to do some
> preprocessing in the implementation file... is this possible? .. or
> does moc only work on header files?

If I remember well, to enable moc processing for file.cpp, it is enough
to add
 #include "file.moc"
at the end of file.cpp and re-run qmake. Then you can - for example -
declare and define a new class with Q_OBJECT, signals, etc, etc in this
file.cpp.

But I may be wrong, because I rarely need this, and it always takes me
some time to get it right.

-- 
 [ signature omitted ]