Pergunta

Why we usually give Q_OBJECT in private section only?
I tried a sample program by giving it in public section ,I found no difference.
If anybody knows can you tell me the reason.

Foi útil?

Solução

By default the Q_OBJECT macro expands to:

#define Q_OBJECT \
public: \
    Q_OBJECT_CHECK \
    static const QMetaObject staticMetaObject; \
    Q_OBJECT_GETSTATICMETAOBJECT \
    virtual const QMetaObject *metaObject() const; \
    virtual void *qt_metacast(const char *); \
    QT_TR_FUNCTIONS \
    virtual int qt_metacall(QMetaObject::Call, int, void **); \
private: \
    Q_DECL_HIDDEN static const QMetaObjectExtraData staticMetaObjectExtraData; \
    Q_DECL_HIDDEN static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **);

at least on mine Qt 4.8, so you can see that it switches to public visibility level to declare the needed functions and the back to private in order to ensure that nothing is broken. So if you declare your Q_OBJECT in a public section you might have the funny consequence that things after are not public anymore :)

Outras dicas

If you put Q_OBJECT under public identifier, all your public declaration will become private. For example:

class Dialog : public QDialog
{    
public:
    Q_OBJECT

    Dialog(QWidget *parent = 0); // <- now declared as private
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top