Hi, Have you tried to use the push/replace directly from the PageStack instead of implementing your own setPage function? This way you do not need to worry about the parenting nor the geometries.
So, something like QMetaObject::invokeMethod(pageStack, "push", Q_ARG(QVariant, qVariantFromValue(mComponent)); I did not try this, but you should be able to pass the component as parameter. If passing a component, the page stack will instantiate the object for you. -P >-----Original Message----- >From: [email protected] [mailto:qt- >[email protected]] On Behalf Of ext Bo >Thorsen >Sent: 16.08.2011 15:15 >To: [email protected] >Subject: [Qt-qml] Adding pages to a PageStack from C++ > >Hi all, > >I'm trying to create a Page in C++ and set it as the current page in a >PageStack. >So far I have tried a lot of different approaches, but none of them work. > >I have a main.qml which I set on a QDeclarativeView. The QML file contains >this: > >Window { > id: window > objectName: "mainWindow" > PageStack { > id: pageStack > objectName: "pageStack" > anchors.fill: parent > } >} > >This is a bit simplified, but I have checked that the behaviour is the same >with >this simple file. > >I create a Page object with the pageStack as parentItem and a Text object with >the page as parentItem in C++. It looks like the pageStack.replace works >correctly, but the text object doesn't show. > >The Page object is instantiated with this code: > >static const QString qml = > "import QtQuick 1.0\n" > "import com.nokia.symbian 1.0\n" > "Page {\n" > " id: %1\n" > " objectName: \"%1\"\n" > " function setPage() {\n" > " parent.replace(%1)\n" > " }\n" > "}"; > >{ > ... > mComponent = new QDeclarativeComponent(engine); > > const QString qmlDeclaration = ::qml.arg(mObjectID); > mComponent->setData(qmlDeclaration.toUtf8(), QUrl()); > > mItem = qobject_cast<QDeclarativeItem*>(mComponent->create()); >} > >The parent is set with this: > >void Displayable::setParentItem(QDeclarativeItem* parent) { > mItem->setParentItem(parent); > mItem->setProperty("anchors.fill", "parent"); } > >The page creation is done by giving it the text object to add: > > QMLPage* page = new QMLPage; > textObject->setParentItem(page); > > QDeclarativeItem* pageStack = > QMLView::getInstance()->rootObject()-> > findChild<QDeclarativeItem*>("pageStack"); > Q_ASSERT(pageStack); > > // This is the Window element from main.qml > QDeclarativeItem* window = pageStack->parentItem(); > Q_ASSERT(window); > > page->setParentItem(pageStack); > > // Call the function from the page declaration > QMetaObject::invokeMethod(page->item(), "setPage"); > >This runs without errors, but the screen is empty. If the Window just has a >Rectangle and I add the text object to that (by setting the rectangle as the >text >parentItem), the text shows. But if I try this with the Page (with the text in >the >page), nothing shows up. > >Any ideas? > >If there are simpler ways to do what I'm working on, I'd love to hear about it. >This UI is completely dynamic, so I can't have anything in the qml files, it >has >to be done in C++. With widget based Qt code, this is trivial. But QML is >certainly not great for this type of application. > >Bo Thorsen, >Fionia Software. > >-- > >Expert Qt and C++ developer for hire >Contact me if you need expert Qt help >http://www.fioniasoftware.dk >_______________________________________________ >Qt-qml mailing list >[email protected] >http://lists.qt.nokia.com/mailman/listinfo/qt-qml _______________________________________________ Qt-qml mailing list [email protected] http://lists.qt.nokia.com/mailman/listinfo/qt-qml
