tags:

views:

26

answers:

1

Hi, I'm new in QT, and I'm just testing out the MOC. For a given class:

class Counter : public QObject
{
 Q_OBJECT
 int m_value;
public:
 Counter() {m_value = 0;}
 ~Counter() {}
 int value() {return m_value;}
public slots:
 void setValue(int value);
signals:
 void valueChanged(int newValue);
};

I want to get a list of all methods in a class, but seem to only be getting a list of signals and slots, although the documentation says it should be all methods? Here's my code:

#include <QCoreApplication>
#include <QObject>
#include <QMetaMethod>
#include <iostream>

using std::cout;
using std::endl;

int main(int argc, char *argv[])
{
 QCoreApplication app(argc, argv);

 const QMetaObject cntmo = Counter::staticMetaObject;
 for(int i = 0; i != cntmo.methodCount(); ++i)
 {
  QMetaMethod qmm(cntmo.method(i));
  cout << qmm.signature() << endl;
 }

 return app.exec();
}

Please beware this is my best c/p, perhaps I forgot to include some headers.

My output:

destroyed(QObject*)
destroyed()
deleteLater()
_q_reregisterTimers(void*)
valueChanged(int)
setValue(int)

Does anyone know why this is happening? Does qt not recognise

int value() {return m_value;}

as a valid method? If so, is there a macro I've forgotten or something like that?

P.S. I'm using 4.6.2

UPDATE

I forgot the implementation of the setValue method, not that it makes too much a difference to my actual question.

void Counter::setValue(int value)
{
    if(value != m_value)
    {
        m_value = value;
        emit valueChanged(value);
    }
}
+1  A: 

As far as I remember you can't access all methods of a QObject subclass through the QMetaObject it provides access only for the signals, slots and INVOCABLE methods:

class MyClass: public QObject {
    Q_OBJECT
    public:
        Q_INVOCABLE int someMethod(const QString &someParam);
};

Maybe it's also provide access to Q_PROPERTY getters and setters. Read articles about Qt object model and meta object system more carefully.

Quotation from QMetaObject class description (http://doc.trolltech.com/4.6/qmetaobject.html#details):

"method() and methodCount() provide information about a class's meta-methods (signals, slots and other invokable member functions)."

There is no information about normal C++ methods access. And actually it's good since reflective techniques are slow.

VestniK
You're right, except that it's Q_INVOKABLE with a K :)
oleks