Results 1 to 7 of 7

Thread: QObject->inherits() troubles: Keeps returning false

  1. #1
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default QObject->inherits() troubles: Keeps returning false

    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.
    Qt Code:
    1. QString iface_type = "IConfigPage";
    2. if (obj->inherits(meta_type.toAscii().data()))
    3. // do some stuff
    To copy to clipboard, switch view to plain text mode 
    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.
    Qt Code:
    1. QString meta_type = "Variable";
    2. if (obj->inherits(meta_type.toAscii().data()))
    3. // do some stuff
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. class QTSF_SHARED_EXPORT Variable : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit Variable(const QString& variableType, QObject *parent = 0);
    7. Variable(const Variable &other);
    8. virtual ~Variable() {};
    9. }
    To copy to clipboard, switch view to plain text mode 
    and below is a variable type based on the Variable base class:
    Qt Code:
    1. class QTSF_SHARED_EXPORT Matrix : public Variable
    2. {
    3. Q_OBJECT
    4.  
    5. friend class MatrixPrivate;
    6. public:
    7. Matrix(QObject *parent = 0);
    8.  
    9. ...
    10. ...
    To copy to clipboard, switch view to plain text mode 
    Any ideas on why it is not working would be much appreciated.
    Thanks
    Jaco

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QObject->inherits() troubles: Keeps returning false

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

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QObject->inherits() troubles: Keeps returning false

    I don't know what magic you do with QTSF_SHARED_EXPORT, but try if your code would work with:
    Qt Code:
    1. if (qobject_cast<Variable *>(obj))
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QObject->inherits() troubles: Keeps returning false

    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)

  5. #5
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: QObject->inherits() troubles: Keeps returning false

    Quote Originally Posted by aamer4yu View Post
    Whats obj in this ?
    That if statement lies within a function which takes a QObject pointer as an argument: function(QObject* obj)

  6. #6
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: QObject->inherits() troubles: Keeps returning false

    Quote Originally Posted by Lykurg View Post
    I don't know what magic you do with QTSF_SHARED_EXPORT, but try if your code would work with:
    Qt Code:
    1. if (qobject_cast<Variable *>(obj))
    To copy to clipboard, switch view to plain text mode 
    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

  7. #7
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: QObject->inherits() troubles: Keeps returning false

    Quote Originally Posted by caduel View Post
    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.

Similar Threads

  1. connect returning false
    By thru in forum Newbie
    Replies: 3
    Last Post: 28th April 2009, 19:58
  2. create a Class inherits from two QObject subclasses
    By sabeesh in forum Qt Programming
    Replies: 17
    Last Post: 31st December 2007, 12:04
  3. QSslSocket : Having troubles
    By Nyphel in forum Newbie
    Replies: 3
    Last Post: 27th June 2007, 14:17
  4. libqjpeg troubles
    By TheRonin in forum Installation and Deployment
    Replies: 6
    Last Post: 25th August 2006, 15:48
  5. lupdate *.pro troubles
    By jeff_s in forum Qt Programming
    Replies: 1
    Last Post: 28th July 2006, 10:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.