Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Questions about the use of QML and C++ and their connection
QtWS25 Last Chance

Questions about the use of QML and C++ and their connection

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 339 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.
  • E Offline
    E Offline
    egusa
    wrote on last edited by
    #1

    I am wondering how I should use QML and C++.
    Each has its own capabilities and advantages, and it is possible to connect them.

    I am currently using QML for screen displays and C++ for calculations and other processing.
    However, I often end up with processes that call the same classes and functions over and over.
    What connections or structures should be made to avoid inadvertent redefinitions?

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

      Can you give a short example? I don't understand what you mean with:

      However, I often end up with processes that call the same classes and functions over and over.
      What connections or structures should be made to avoid inadvertent redefinitions?

      (Z(:^

      E 1 Reply Last reply
      0
      • sierdzioS sierdzio

        Can you give a short example? I don't understand what you mean with:

        However, I often end up with processes that call the same classes and functions over and over.
        What connections or structures should be made to avoid inadvertent redefinitions?

        E Offline
        E Offline
        egusa
        wrote on last edited by
        #3

        @sierdzio
        If I try to use a C++ class from QML.
        If I use that class in a class on another C++, it will be initialized again.
        In such a case, the class can no longer be used from QML.

        sierdzioS 1 Reply Last reply
        0
        • KH-219DesignK Offline
          KH-219DesignK Offline
          KH-219Design
          wrote on last edited by
          #4

          I'm only taking a wild guess here....

          But if the tactic you're using for exposing the C++ object to QML involves something like this:

          // assume: QQmlEngine* engine 
          // assume: "this" is your custom C++ subtype of QObject that you expose to QML
          
              engine->rootContext()->setContextProperty( "myCustomViewModel", this );
          

          Then if you are doing that each time you instantiate an instance of your type, then you would be overwriting the reference that your QML has for "myCustomViewModel".

          This is all wild conjecture on my part, because we really need more code snippets to understand. However, my conjecture matches your real-life codebase, then the solution is to de-couple instantiation from setContextProperty. Then you could instantiate many objects, but only setContextProperty on one to export that one to QML.

          www.219design.com
          Software | Electrical | Mechanical | Product Design

          1 Reply Last reply
          1
          • E egusa

            @sierdzio
            If I try to use a C++ class from QML.
            If I use that class in a class on another C++, it will be initialized again.
            In such a case, the class can no longer be used from QML.

            sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            @egusa said in Questions about the use of QML and C++ and their connection:

            @sierdzio
            [...] it will be initialized again. [...]

            Well, that part is up to you, really :-) You can take the instance from QML (pass it via signal, for example), or you can use your C++ object as a singleton and do the initialization on C++ part. Or you can init your object in main.cpp, pass it to QML as context property, then pass the same pointer down your C++ code to where you need it. There are many ways to get around this problem.

            (Z(:^

            1 Reply Last reply
            1

            • Login

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