View Full Version : problem with qobject_cast<QWidget *>(object) (from QML object)

5th May 2011, 01:24
qmlView = new QDeclarativeView;
qmlView->setSource( QUrl::fromLocalFile( "modaldialog.qml" ) );
QObject *object = qmlView->rootObject();

QMetaObject::invokeMethod( object, "dialog_type_02",
Q_RETURN_ARG( QVariant, returnedValue ),
Q_ARG( QVariant, dlg_text )

QWidget *widget = qobject_cast<QWidget *>(object);//doesn't work, return null
layout->addWidget( widget );//doesn't work, above is null

//however if i replace the above 2 lines of code with this:
QGraphicsObject* graphicsObject =qobject_cast<QGraphicsObject *>(object); //works fine
scene.addItem( graphicsObject ); //works fine

why qobject_cast works ok for a QGraphicsObject and not for a QWidget coming from QML?

Why use the "layout->addWidget(...)" option? because i want a QDialog with the stuff coming from QML

5th May 2011, 09:08

Returns the given object cast to type T if the object is of type T (or of a subclass); otherwise returns 0. If object is 0 then it will also return 0.
QGraphicsObject is NOT a QWidget subclass which is why:

QWidget *widget = qobject_cast<QWidget *>(object);
wont work.

because i want a QDialog with the stuff coming from QML
This technology does not exist yet.
You can have widgets in QGraphicsView, but you can't have QGraphicsObject as QWidgets. (unless these are QProxyWidget s, which means they have a QWidget counterparts to start with)

6th May 2011, 06:56
thanks,very much.