Hi,
Please can someone tell me why when I call a QML function (openInfoBanner) from C++ it doesn't open the InfoBanner?
this is the code of my test app:
main.cpp
Code:#include <QtGui/QApplication> #include <QtDeclarative> #include "qmlapplicationviewer.h" #include "myclass.h" Q_DECL_EXPORT int main(int argc, char *argv[]) { QScopedPointer<QApplication> app(createApplication(argc, argv)); QmlApplicationViewer *viewer = new QmlApplicationViewer(); viewer->setMainQmlFile(QLatin1String("qml/test022/main.qml")); viewer->showExpanded(); MyClass myClass; myClass.openBanner("Hello Banner"); return app->exec(); }
myclass.h
Code:#ifndef MYCLASS_H #define MYCLASS_H #include <Qt> #include <QObject> #include <QtDeclarative> #include <QDeclarativeEngine> #include <QDeclarativeContext> class MyClass : public QObject { Q_OBJECT public: MyClass(){} Q_INVOKABLE void openBanner(QVariant txt){ QDeclarativeEngine *engine = new QDeclarativeEngine; QDeclarativeComponent component(engine, QUrl::fromLocalFile("qml/test022/MainPage.qml")); if (component.status() == 1){ QObject *object = component.create(); QMetaObject::invokeMethod(object, "openInfoBanner", Q_ARG(QVariant, txt)); object->deleteLater(); } engine->deleteLater(); } }; #endif // MYCLASS_H
main.qml
Code:import QtQuick 1.1 import com.nokia.symbian 1.1 import com.nokia.extras 1.1 PageStackWindow { id: window initialPage: mainPg showStatusBar: true showToolBar: true MainPage { id: mainPg parent: window tools: toolBarLayout } ToolBarLayout { id: toolBarLayout ToolButton { flat: true iconSource: "toolbar-back" onClicked: window.pageStack.depth <= 1 ? Qt.quit() : window.pageStack.pop() } } }
MainPage.qml
Code:import QtQuick 1.1 import com.nokia.symbian 1.1 import com.nokia.extras 1.1 Page { id: mainPage function openInfoBanner(txt){ console.log("openInfoBanner(txt) executed") //this log msg is shown correctly textCenter.text = txt; infBanner.text = txt; infBanner.open(); console.log("infBanner must been opened now!") //this log msg is shown correctly but the infBanner doesn't opened and //the textCenter text doesn't changed! } InfoBanner{ id: infBanner timeout: 3000 } Text { id: textCenter anchors.centerIn: parent text: qsTr("Hello world!") color: platformStyle.colorNormalLight font.pixelSize: 20 } }


Reply With Quote


