PDA

View Full Version : cannot convert 'QGraphicsObject*' to 'QObject*' in initialization



rwtmoore@hotmail.com
28th January 2013, 15:30
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.

d_stranz
28th January 2013, 16:26
Use qobject_cast on the pointer returned by rootObject():



QObject * object = qobject_cast< QObject * >( view.rootObject() );


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

rwtmoore@hotmail.com
28th January 2013, 19:04
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.

wysota
28th January 2013, 19:26
You're missing:


#include <QGraphicsObject>