Results 1 to 4 of 4

Thread: cannot convert 'QGraphicsObject*' to 'QObject*' in initialization

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2013
    Posts
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default cannot convert 'QGraphicsObject*' to 'QObject*' in initialization

    I got the following code from http://apidocs.meego.com/1.2/qt4/qtbinding.html (and many other places):


    // Using QDeclarativeView
    QDeclarativeView view;
    view.setSource(QUrl::fromLocalFile("MyItem.qml"));
    view.show();
    QObject *object = view.rootObject();


    Unfortunately it doesn't compile. I get this error message:

    cannot convert 'QGraphicsObject*' to 'QObject*' in initialization

    It looks like we're trying to cast a QGraphicsObject to a QObject. What is going on here?

    Thanks in advance.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,349
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: cannot convert 'QGraphicsObject*' to 'QObject*' in initialization

    Use qobject_cast on the pointer returned by rootObject():

    Qt Code:
    1. QObject * object = qobject_cast< QObject * >( view.rootObject() );
    To copy to clipboard, switch view to plain text mode 

    As always, check the pointer returned by this operation to ensure it is non-NULL.

Similar Threads

  1. Replies: 2
    Last Post: 24th December 2015, 13:33
  2. animating a QGraphicsObject
    By tommy22 in forum Qt Programming
    Replies: 4
    Last Post: 7th January 2013, 11:20
  3. QGraphicsObject Performance Issue
    By alizadeh91 in forum Qt Programming
    Replies: 0
    Last Post: 13th March 2012, 17:41
  4. protected QGraphicsObject
    By stefan in forum Qt Programming
    Replies: 6
    Last Post: 19th August 2011, 17:04
  5. Replies: 7
    Last Post: 15th December 2009, 10:01

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.