On Mon, 15 Aug 2011 15:29:21 ext Zhu Qi Chao wrote:
> Hi All,
> I am trying to rewrite the game logic in SameGame example through C++ code.
> But I suck in createBlocks() function where I can create the components
> successfully but they can't display on the view. Original funciton in
> samegame.js
> 
> 1.                        function createBlock()
> 
> 2.                        {
> 
> 3.                            if (component.status == Component.Ready)
> 
> 4.                            {
> 
> 5.                                var dynamicObject =
> component.createObject(gameCanvas);
> 
> 6.                                ...
> 
> 7.                        }
> My C++ function
> 
> 1.                        void GameData::createBlock()
> 
> 2.                        {
> 
> 3.                            component = new
> QDeclarativeComponent(parentView->engine(),
> QUrl::fromLocalFile(blockSrc));
> 
> 4.                            QDeclarativeContext* context =
> parentView->rootContext();
> 
> 5.                            if (component->status() ==
> QDeclarativeComponent::Ready)
> 
> 6.                            {
> 
> 7.                                QObject* dynamicObject =
> component->create(context);
> 
> 8.                                ....
> }
> 1.                   Hi All,
> I am trying to rewrite the game logic in SameGame example through C++ code.
> But I suck in createBlocks() function where I can create the components
> successfully but they can't display on the view.
> 
> Original funciton in samegame.js
> Code:
> 
> function createBlock()
> 
> {
> 
>     if (component.status == Component.Ready)
> 
>     {
> 
>         var dynamicObject = component.createObject(gameCanvas);
> 
>         ...
> 
> }
> 
> My C++ function
> Code:
> 
> void GameData::createBlock()
> 
> {
> 
>     component = new QDeclarativeComponent(parentView->engine(),
> QUrl::fromLocalFile(blockSrc));
> 
>     QDeclarativeContext* context = parentView->rootContext();
> 
>     if (component->status() == QDeclarativeComponent::Ready)
> 
>     {
> 
>         QObject* dynamicObject = component->create(context);
> 
>         ....
> 
> }
> I only add some "objectName" property in the .qml files so that it can help
> me to find child item. Then almost everything left unmodified in whole QML
> files.
> 
> From qDebug() I can see that dynamicObject has been created well. But I
> can't see any blocks in the view. What's wrong? If the component can't be
> built on proper QDeclarativeContext, couldn't it be displayed well, just
> like old style QWidget and QGraphicsScene hierarchies?

It is much like the old QGraphicsItem hierarchies, but the context part is 
different. This is because the context is used for the declarative bindings 
but is not an item in the item tree. So it looks like you've created an object 
in the correct context, but you haven't set its parent item yet. Without a 
parent item it will not be visible in the scene, just like with QGraphicsItem.

You'll have to cast it to a QDeclarativeItem*, and then call 
setParentItem(gameCanvas) (where gameCanvas is the QDeclarativeItem 
corresponding to gameCanvas in the samegame example.

-- 
Alan Alpert
Senior Engineer
Nokia, Qt Development Frameworks
_______________________________________________
Qt-qml mailing list
[email protected]
http://lists.qt.nokia.com/mailman/listinfo/qt-qml

Reply via email to