PDA

View Full Version : QObject->inherits() troubles: Keeps returning false



JPNaude
3rd March 2010, 07:43
Hi

I use the QObject->inherits() function in a couple of places, in the one place it works but in the other place it does not and I can't figure out why.

First usage case = works
I return a list of objects which implements a specific interface. In obj the interface is declared using Q_INTERFACES, but according to the documentation inherits is not meant only for interfaces.


QString iface_type = "IConfigPage";
if (obj->inherits(meta_type.toAscii().data()))
// do some stuff

Second usage case = does not work
I filter subjects in order to allow only certain types of objects to continue past the if statement. Variable is the name of a base class in my project.


QString meta_type = "Variable";
if (obj->inherits(meta_type.toAscii().data()))
// do some stuff

Since these two are almost exactly the same, I suspect that it is related to class Variable. A simplified version of this class is shown below:


class QTSF_SHARED_EXPORT Variable : public QObject
{
Q_OBJECT

public:
explicit Variable(const QString& variableType, QObject *parent = 0);
Variable(const Variable &other);
virtual ~Variable() {};
}

and below is a variable type based on the Variable base class:


class QTSF_SHARED_EXPORT Matrix : public Variable
{
Q_OBJECT

friend class MatrixPrivate;
public:
Matrix(QObject *parent = 0);

...
...

Any ideas on why it is not working would be much appreciated.
Thanks
Jaco

aamer4yu
3rd March 2010, 08:01
QString meta_type = "Variable";
if (obj->inherits(meta_type.toAscii().data()))
// do some stuff
Whats obj in this ?

Lykurg
3rd March 2010, 08:16
I don't know what magic you do with QTSF_SHARED_EXPORT, but try if your code would work with:
if (qobject_cast<Variable *>(obj))

caduel
3rd March 2010, 09:08
check if moc is run for those classes
(the 3 ingredients for the qt magic to work are: derive from QObject, do not forget Q_OBJECT macro, *and* run moc - e.g. by putting the header file inside the .pro file's HEADERS section)

JPNaude
3rd March 2010, 09:38
Whats obj in this ?

That if statement lies within a function which takes a QObject pointer as an argument: function(QObject* obj)

JPNaude
3rd March 2010, 09:40
I don't know what magic you do with QTSF_SHARED_EXPORT, but try if your code would work with:
if (qobject_cast<Variable *>(obj))


Hmmm, the code lies in a library, and QTSF_SHARED_EXPORT is defined as follows:

#if defined(QTSF_LIBRARY)
# define QTSF_SHARED_EXPORT Q_DECL_EXPORT
#else
# define QTSF_SHARED_EXPORT Q_DECL_IMPORT
#endif

So it should not make a different I believe

JPNaude
3rd March 2010, 09:42
check if moc is run for those classes
(the 3 ingredients for the qt magic to work are: derive from QObject, do not forget Q_OBJECT macro, *and* run moc - e.g. by putting the header file inside the .pro file's HEADERS section)

Thanks. I double checked and moc_* files are created for both Variable and Matrix classes. So it does not seem like this is the problem.