How many QDeclarativeEngines and QDeclarativeView?
-
Hi,
I am working with QML and C++ and I am wondering what is the good way to instance many QML objects from C++.
First, to instance an object I need a QDeclarativeEngine that I use to create the QDeclarativeComponent (which I cast later). Like this:
@
QDeclarativeEngine engine;
QDeclarativeComponent component(&engine, QUrl("example.qml"));
Node *node = qobject_cast<Node *>(component.create());
@This works perfectly, but I was wondering: if I want to create many instances of my object (lets say 100), should I use the same engine for all of them or do I have to instance a new engine everytime?
Also, I want to display those items (I have implemented a print function), so I have a QDeclarativeView and it seems that (from what I have read on the internet) there should be only one QDeclarativeView per program. So i supose that the view is always the same like this:
@
QDeclarativeView* view = new QDeclarativeView;
view->setSource(QUrl::fromLocalFile("Base.qml"));
node->setParentItem(qobject_cast<QDeclarativeItem*>(view->rootObject()));
view->show();
@Am I right?
Well, I have a question: I could make my view "dynamic". I mean that eventhough I change the color or my item (for example) after doing @view->show()@, it is displayed as with the new color, but I want it to be displayed with the old color and then with the new color. And also, this problem means that I cannot drow instances of my object dynamcly, they all apear at the same time even if I declare them later on the code (I tried to do sleep(2) and it didn't work either...)Thank you very much for your answers!
-
Hi,
Firstly, you really should only have one QDeclarativeEngine.
Secondly, a QDeclarativeView already instantiates a QDeclarativeEngine, so if you wish to instantiate objects you should use your view's engine, rather than creating a new one on the stack or whatever.
Thirdly, everything is driven by the event loop. So if you show() and then change a color, and then call exec() of course the color you see will be the one you set prior to returning to the event loop by calling exec(). Sleeping blocks the event loop, so that won't help.It appears that what you want to do, is use signal handlers or some other event-driven programming mechanism, to draw stuff dynamically. Read up on Qt's events and signals/slots for more information about how to do this.
Cheers,
Chris.