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 can serveral same qml objects references to one C++ object?
QtWS25 Last Chance

How can serveral same qml objects references to one C++ object?

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 3 Posters 1.2k Views
  • 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.
  • J Offline
    J Offline
    jackmack
    wrote on last edited by
    #1

    Hi together,

    I have a question about instantiating several qml objects but only references to one C++ object.
    I have written a C++ object for displaying a webcam using openCV. I have successfully registered the C++ object and I'm able to embed it in qml.

    My C++ application (using QDeclativeView) uses QML as it's UI. The UI is a flickable one and you can switch between several pages. Some pages embeds the C++ object.

    The problem: Every embedded C++ object has its own C++ instance!

    I want to have that every embedded C++ object references only to one C++ instance.

    Example:

    test.qml:

    Row {
    spacing: 5

    MyCamera {
    id: camera1
    with: 100
    height: 100
    number: 0
    }

    MyCamera {
    id: camera2
    with: 100
    height: 100
    number: 0
    }
    }

    Result:
    The constructor of the C++ class is called twice.

    What I want:
    Camera1 and Camera2 references to one C++ instance.

    Why?
    My "logical layer" which instantiates the C++ object "paints" something into the one and only camera widget. And "Camera1" and "Camera2" should be displaying it.

    Thx
    jackmack

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      z.emb
      wrote on last edited by
      #2

      You create two MyCamera items. The qml engine instantiates the items. So there are two C++ instances of your QQuickItem subclass.

      You can add your "logical layer" to the qml context through the qml view so the qml items can interact with its properties.

      1 Reply Last reply
      0
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #3

        This is how QML works. Every component you declare is a separate instance.

        You could experiment with transforming your class into a singleton, or you can instantiate the component in C++ and passing instances to GUI, or maybe a clever use of Loader could help:
        @
        MyCamera {
        id: globalCamObject
        }

        Loader {
        id: camera1
        sourceComponent: globalCamObject
        }

        Loader {
        id: camera2
        sourceComponent: globalCamObject
        }
        @

        (Z(:^

        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