Results 1 to 8 of 8

Thread: qobject_cast<T> in shared libraries...

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qobject_cast<T> in shared libraries...

    I personally think that you should report your suggestion to the Qt guys, it will be nice to have it in Qt 4.5.

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

    Default Re: qobject_cast<T> in shared libraries...

    This (known) "issue" (or feature, if you want; probably this implementation is faster and was chosen with purpose) can be worked around by having the common base class put inside a lib that both "parties" link against. That way there will be only one such metaobject and the qobject_cast will not fail.

    Still, reporting it to the trolls won't hurt.

    HTH
    PS: There are (some) compilers that internally use a string-compare for dynamic_cast, too.
    g++, however, uses (for efficiency reasons - again) a pointer comparison of the internal rtti object. Thus, dynamic_cast may (and with g++ does) suffer from across shared library issues, too.

  3. #3
    Join Date
    Dec 2008
    Posts
    29
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: qobject_cast<T> in shared libraries...

    Thanks for answer,

    Quote Originally Posted by caduel View Post
    This (known) "issue" (or feature, if you want; probably this implementation is faster and was chosen with purpose) can be worked around by having the common base class put inside a lib that both "parties" link against. That way there will be only one such metaobject and the qobject_cast will not fail.
    I thougth, that this common base class can be also QObject, because both sides (plugins) link against Qt libraries. Or am I wrong?

  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_cast<T> in shared libraries...

    No. (Only if you try the rather pointless qobject_cast<Qbject*>(...) ;-)

    If you do a qobject_cast<T*>(someObj) then the definition of T has to be in a common library. You must not have the header of T added in two .pro files' HEADERS-sections. Otherwise the Q_OBJECT macro will be moced and compiled multiple times. This would lead to multiple instances of the metaobject and the problem you noted.
    If you put the definition of T into a lib there will be only one metaobject (the one defined by the lib) and the qobject_cast will work as expected.

    HTH

  5. #5
    Join Date
    Dec 2008
    Posts
    29
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: qobject_cast<T> in shared libraries...

    Quote Originally Posted by caduel View Post
    You must not have the header of T added in two .pro files' HEADERS-sections. Otherwise the Q_OBJECT macro will be moced and compiled multiple times. This would lead to multiple instances of the metaobject and the problem you noted.
    Thank you, this is the key! Interface header file was included in both plugin projects and that led to 2x different static objects for "one class". Shame on me
    Last edited by seim; 31st December 2008 at 03:12.

  6. #6
    Join Date
    Dec 2008
    Posts
    29
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Thumbs up Re: qobject_cast<T> in shared libraries...

    One more thing as a solution given by B.Poulain:

    "The problem come from the declaration of the
    class IObject without the macro Q_DECLARE_INTERFACE. This macro should
    be used for all objects exported outside the plugin.
    If you add it, everything works fine.

    If you look at the declaration of Q_DECLARE_INTERFACE, it use a slightly
    different approach to make qobject_cast working across library but
    without being dependant of the linker."

    The IObject header file has to be added in both .pro files.

    Thank you all, for your posts.


Similar Threads

  1. Deployment (shared libraries)
    By amagdy.ibrahim in forum Qt Programming
    Replies: 5
    Last Post: 26th June 2008, 21:55
  2. Related to Packaging of Qt4 Application
    By archanasubodh in forum Installation and Deployment
    Replies: 1
    Last Post: 21st March 2008, 14:55
  3. Qt 3.3 libraries
    By ToddAtWSU in forum Qt Programming
    Replies: 1
    Last Post: 21st December 2006, 17:25
  4. Shared lib template broken under linux ???
    By fullmetalcoder in forum Qt Programming
    Replies: 9
    Last Post: 26th April 2006, 20:05
  5. I got two problems when I used static compiled library of QT4
    By qintm in forum Installation and Deployment
    Replies: 8
    Last Post: 20th April 2006, 08:52

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
  •  
Qt is a trademark of The Qt Company.