Results 1 to 4 of 4

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

  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,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    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.

  3. #3
    Join Date
    Jan 2013
    Posts
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

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

    Thanks for the reply. I already tried that (although the dozens of examples I've seen don't cast ). It takes care of the compiler error but it crashes at runtime at this line:

    QDeclarativeView view;

    Here's the error message:

    Qml debugging is enabled. Only use this in a safe environment!
    QPixmap: Cannot create a QPixmap when no GUI is being used
    QWidget: Cannot create a QWidget when no GUI is being used

    This application has requested the Runtime to terminate it in an unusual way.
    Please contact the application's support team for more information.
    Press <RETURN> to close this window...


    I also tried adding:

    #include <QDeclarativeItem>

    to the top of the file. It's also gets rid of the compiler error but crashes at runtime, same as above.


    Added after 25 minutes:


    I should mention that in a simple app I was able to use

    #include <QApplication>
    #include <QDeclarativeItem>
    #include "qmlapplicationviewer.h"


    But in my current app, this doesn't compile

    #include <QApplication>

    It only recognizes:

    #include <QCoreApplication>

    The problem is that I don't want to use qmlapplicationviewer.
    Last edited by rwtmoore@hotmail.com; 28th January 2013 at 19:04.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

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

    You're missing:

    Qt Code:
    1. #include <QGraphicsObject>
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.