hello,
I have an application, in which i am using the same approach.
now if i want to access the properties of baseObj, which meant to be parent of my dynamically created object, what should i do.
Here is example of my code..
main.qml
import FirstButtonClick 1.0
Rectangle{
property string myString : "My String"
Text{
text : "main file"
}
MouseArea{
onClicked:{
buttonClick.openPage("qrc:qml/FirstButton.qml")
}
}
FirstButtonClick{
id : buttonClick
}
}
FirstButton.qml
Rectangle{
Text{
text : "FirstButton Page"
}
Component.onCompleted{
console.log("main.qml property : " + myString // main.qml property)
}
}
main.cpp
#include "FirstButtonClick.h"
#include "main.h"
int main(int argc, char *argv[])
{
qmlRegisterType<>("FirstButtonClick", 1, 0, "FirstButtonClick");
QApplication app(argc, argv);
m_view = new QDeclarativeView;
m_view.setSource(QUrl("qrc:qml/main.qml"));
m_view.showFullScreen();
return app.exec();
}
main.h
QDeclarativeView* m_view;
FirstButtonClick.h
extern QDeclarativeView* m_view;
class FirstButtonClick : public OQbject
{
Q_OBJECT;
public :
Q_INVOKABLE QObject* openPage(QString fileName);
}
FirstButtonClick.cpp
#include "FirstButtonClick.h"
QObject* FirstButtonClick :: openPage(QString fileName)
{
QDeclarativeContext* context = m_view->engine()->rootContext();
QDeclarativeComponent* component = new QDeclarativeComponent(m_view->engine(), QUrl(fileName), m_view->engine());
QObject * object = component->create();
QDeclarativeItem* myDialog = qobject_cast<QDeclarativeItem*>(object);
QDeclarativeItem *rootitem = qobject_cast<QDeclarativeItem*>(m_view->rootObject());
myDialog->setParentItem(rootitem);
return object;
}
the problem with the above code is FirstButton.qml page is loaded but when it tries to access the property of main.qml ie. myString it cant access it.
suggest me me a way to use it properly