Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How many QDeclarativeEngines and QDeclarativeView?
Forum Updated to NodeBB v4.3 + New Features

How many QDeclarativeEngines and QDeclarativeView?

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    excalibur1491
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • C Offline
      C Offline
      chrisadams
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved