📄 moc.1
字号:
.TH moc 1 "24 June 2001" "Trolltech AS" \" -*- nroff -*-.\".\" $Id: qt/moc.1 3.1.1 edited Nov 7 16:55 $.\".\" Copyright 1992-2002 Trolltech AS. All rights reserved..\".\" This file is part of Qt and may be distributed and used according to.\" the terms and conditions described in the LICENSE file..\".nh.SH NAMEmoc \- generate Qt meta object support code.SH SYNOPSIS.B moc[-o file] [-i] [-f] [-k] [-ldbg] [-nw] [-p path] [-q path] [-v] file.SH DESCRIPTIONThis page documents the.B Meta Object Compilerfor the Qt GUI application framework. The.B mocreads one or more C++ class declarations from a C++ header or sourcefile and generates one C++ source file containing meta objectinformation for the classes. The C++ source file generated by the.B mocmust be compiled and linked with the implementation of the class (or itcan be #included into the class's source file)..PPIf you use .B qmaketo create your Makefiles, build rules will be included that call the.B mocwhen required, so you will not need to use the .B mocdirectly..PPIn brief, the meta object system is a structure used by Qt (see.BR http://doc.trolltech.com ")"for component programming and run time type information. It addsproperties and inheritance information to (some) classes andprovides a new type of communication between those instances of thoseclasses, signal-slotconnections..SH OPTIONS.TP.I "-o file"Write output to.I filerather than to stdout..TP.I -fForce the generation of an #include statement in the output.This is the default for files whose name matches the regularexpression .[hH][^.]* (i.e. the extension starts with.B Hor.B h). Thisoption is only useful if you have header files that do not follow thestandard naming conventions..TP.I "-i"Do not generate an #include statement in the output. This may be usedto run.B mocon a C++ file containing one or more class declarations. You should then#include the meta object code in the .cpp file (see USAGE below). If both.I -fand.I -iare present, the last one wins..TP.I "-nw"Do not generate any warnings. Not recommended..TP.I "-ldbg"Write a flood of lex debug information to stdout..TP.I "-p path"Makes.B mocprepend.IR path /to the file name in the generated #include statement (if one is generated)..TP.I "-q path"Makes.B mocprepend.IR path /to the file name of qt #include files in the generated code..TP.I "-v"Displays the version of.B mocand Qt..PPYou can explicitly tell the.B mocnot to parse parts of a headerfile. It recognizes any C++ comment (//) that contains the substringsMOC_SKIP_BEGIN or MOC_SKIP_END. They work as you would expect and youcan have several levels of them. The net result as seen by the.B mocis as if you had removed all lines between a MOC_SKIP_BEGIN and aMOC_SKIP_END.SH USAGE.B mocis almost always invoked by.BR make (1),not by hand..PP.B mocis typically used with an input file containing class declarationslike this:.PP.in +4.nfclass YourClass : public QObject { Q_OBJECT Q_PROPERTY( ... ) Q_CLASSINFO( ... )public: YourClass( QObject * parent=0, const char * name=0 ); ~YourClass();signals:public slots:};.fi.in -4.PPHere is a useful makefile rule if you only use GNU make:.PP.in +4.nfm%.cpp: %.h moc $< -o $@.fi.in -4.PPIf you want to write portably, you can use individual rules of thefollowing form:.PP.in +4.nfmNAME.cpp: NAME.h moc $< -o $@.fi.in -4.PPYou must also remember to add.I mNAME.cppto your SOURCES (substitute your favorite name) variable and.I mNAME.oto your OBJECTS variable..PP(While we prefer to name our C++ source files .cpp, the.B mocdoesn't know that, so you can use .C, .cc, .CC, .cxx or even .c++ ifyou prefer.).PPIf you have class declarations in C++ files, we recommend that you usea makefile rule like this:.PP.in +4.nfNAME.o: mNAME.cppmNAME.cpp: NAME.cpp moc -i $< -o $@.fi.in -4.PPThis guarantees that.BR make (1)will run the.B mocbefore it compiles.IR NAME.cpp .You can then put.PP.ti +4#include "nNAME.cpp".PPat the end of.IR NAME.cpp ,where all the classes declared in that file are fully known..SH DIAGNOSTICSSometimes you may get linkage errors, saying thatYourClass::className() is undefined or that YourClass lacks a vtbl.Those errors happen most often when you forget to compile themoc-generated C++ code or include that object file in the linkcommand..PPThe.B mocwill warn you about a number of dangerous or illegal constructs..SH BUGSThe.B mocdoes not expand #include or #define, it simply skips any preprocessordirectives it encounters. This is regrettable, but is normally not aproblem in practice.The.B mocdoes not handle all of C++. The main problem is that class templatescannot have signals or slots. This is an important bug. Here is anexample:.PP.in +4.nfclass SomeTemplate<int> : public QFrame { Q_OBJECT ....signals: void bugInMocDetected( int );};.fi.in -4.PPLess importantly, the following constructs are illegal. All of themhave have alternatives which we think are usually better, so removingthese limitations is not a high priority for us..SS "Multiple inheritance requires QObject to be first."If you are using multiple inheritance, .B moc assumes that the.B firstinherited class is a subclass of QObject. Also, be sure that.B onlythe first inherited class is a QObject..PP.in +4.nfclass SomeClass : public QObject, public OtherClass { ...};.fi.in -4.PPThis bug is almost impossible to fix; since the.B mocdoes not expand#include or #define, it cannot find out which one of the base classes is aQObject..SS "Function pointers cannot be arguments to signals or slots."In most cases where you would consider that, we think inheritance is abetter alternative. Here is an example of illegal syntax:.PP.in +4.nfclass SomeClass : public QObject { Q_OBJECT ...public slots: // illegal void apply( void (*apply)(List *, void *), void * );};.fi.in -4.PPYou can work around this restriction like this:.PP.in +4.nftypedef void (*ApplyFunctionType)( List *, void * );class SomeClass : public QObject { Q_OBJECT ...public slots: void apply( ApplyFunctionType, char * );};.fi.in -4.PPIt may sometimes be even better to replace the function pointer withinheritance and virtual functions, signals or slots..SS "Friend declarations cannot be placed in signals or slots sections"Sometimes it will work, but in general, friend declarations cannot beplaced in.B signalsor.B slotssections. Put them in the good old.BR private ", " protectedor.B publicsections instead. Here is an example of the illegal syntax:.PP.in +4.nfclass SomeClass : public QObject { Q_OBJECT ...signals: friend class ClassTemplate<char>; // illegal};.fi.in -4.SS "Signals and slots cannot be upgraded"The C++ feature of upgrading an inherited member function to.B publicstatus is not extended to cover signals and slots. Here is an illegalexample:.PP.in +4.nfclass Whatever : public QButtonGroup { ...public slots: QButtonGroup::buttonPressed; // illegal ...};.fi.in -4.PPThe QButtonGroup::buttonPressed() slot is protected..PPC++ quiz: What happens if you try to upgrade a protected memberfunction which is overloaded?.IP- All the functions are upgraded..IP- That is not legal C++..\" Good idea, but look in the SEE ALSO section....SS "Type macros cannot be used for signal and slot arguments"Since the.B mocdoes not expand #define, type macros that take an argumentwill not work in signals and slots. Here is an illegal example:.PP.in +4.nf#ifdef ultrix#define SIGNEDNESS(a) unsigned a#else#define SIGNEDNESS(a) a#endifclass Whatever : public QObject { ...signals: void someSignal( SIGNEDNESS(int) ); // illegal};.PPA #define without arguments works..fi.in -4.SS "Nested classes cannot be in the signals or slots sections nor have signals or slots"Here's an example:.PP.in +4.nfclass A { Q_OBJECTpublic: class B { public slots: // illegal void b(); ... };signals: class B { // illegal void b(); ... }:};.fi.in -4.PP.SS "Constructors cannot be used in signals or slots sections"It is a mystery to us why anyone would put a constructor on either the.B signalsor.B slotssections. You can't, anyway (except that it happens to work in somecases). Put them in.BR private ", " protectedor.B publicsections, where they belong. Here is an example of the illegal syntax:.PP.in +4.nfclass SomeClass : public QObject { Q_OBJECTpublic slots: SomeClass( QObject *parent, const char *name ) : QObject( parent, name ) {} // illegal ...};.fi.in -4.SS "Properties need to be declared before the public section that contains the respective get and set functions".PPDeclaring the first property within or after the public section thatcontains the type definition and the respective get and set functionsdoes not work as expected. The.B mocwill complain that it can neitherfind the functions nor resolve the type. Here is an example of theillegal syntax:.PP.in +4.nfclass SomeClass : public QObject { Q_OBJECTpublic: ... // illegal Q_PROPERTY( Priority priority READ priority WRITE setPriority ) Q_ENUMS( Priority ) enum Priority { High, Low, VeryHigh, VeryLow }; void setPriority( Priority ); Priority priority() const; ...};.fi.in -4.PPWork around this limitation by declaring all properties at thebeginning of the class declaration, right after Q_OBJECT:.PP.in +4.nfclass SomeClass : public QObject { Q_OBJECT Q_PROPERTY( Priority priority READ priority WRITE setPriority ) Q_ENUMS( Priority )public: ... enum Priority { High, Low, VeryHigh, VeryLow }; void setPriority( Priority ); Priority priority() const; ...};.fi.in -4.PP.SH "SEE ALSO".BR http://www.trolltech.com ", ".BR "C++ ARM, section r.11.3" " (for the answer to the quiz), and".BR http://doc.trolltech.com " (for complete Qt documentation)."
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -