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. c++ class that adds a drawable qquickitem
Qt 6.11 is out! See what's new in the release blog

c++ class that adds a drawable qquickitem

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 2 Posters 1.0k 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.
  • S Offline
    S Offline
    spiderdab
    wrote on last edited by spiderdab
    #1

    ..not a good title, I'm sorry..
    I'm reading a lot in these days, but couldn't find what I need, but I think it is simpler that I imagine.

    I need dynamically created objects, but those objects are both a c++ class and a drawable item.

    From instructions taken HERE I can dynamically create qml objects. and it works.

    Next I understand that to be able to create the class in runtime, when I need, the right way is to use

    qmlRegisterType<MyClass>("myClass", 1, 0, "MyClass");
    

    ... and it works too..

    I've read that to have the possibility of draw objects, I have to derive my c++ class from QQuickItem, instead of QObject, but I didn't find any example, nor how to implement this.

    Can you help me?
    Let's say that I have a button. every time I click that I want to add a c++ object class to a QList and draw a Rectangle with information inside. After that object variables are changed in main.cpp, and visualization should change accordingly.

    GrecKoG 1 Reply Last reply
    0
    • S spiderdab

      ..not a good title, I'm sorry..
      I'm reading a lot in these days, but couldn't find what I need, but I think it is simpler that I imagine.

      I need dynamically created objects, but those objects are both a c++ class and a drawable item.

      From instructions taken HERE I can dynamically create qml objects. and it works.

      Next I understand that to be able to create the class in runtime, when I need, the right way is to use

      qmlRegisterType<MyClass>("myClass", 1, 0, "MyClass");
      

      ... and it works too..

      I've read that to have the possibility of draw objects, I have to derive my c++ class from QQuickItem, instead of QObject, but I didn't find any example, nor how to implement this.

      Can you help me?
      Let's say that I have a button. every time I click that I want to add a c++ object class to a QList and draw a Rectangle with information inside. After that object variables are changed in main.cpp, and visualization should change accordingly.

      GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      @spiderdab said in c++ class that adds a drawable qquickitem:

      Let's say that I have a button. every time I click that I want to add a c++ object class to a QList and draw a Rectangle with information inside. After that object variables are changed in main.cpp, and visualization should change accordingly.

      Why do you need a C++ QQuickItem for that?
      All that can be done with a c++ model and a view (ListView, Repeater, ...) with delegates in QML.

      I would also recommend not to use Qt.createQmlObject or Qt.createComponent from your link (too imperative) and stay on the declarative side by using models and views.

      S 1 Reply Last reply
      2
      • GrecKoG GrecKo

        @spiderdab said in c++ class that adds a drawable qquickitem:

        Let's say that I have a button. every time I click that I want to add a c++ object class to a QList and draw a Rectangle with information inside. After that object variables are changed in main.cpp, and visualization should change accordingly.

        Why do you need a C++ QQuickItem for that?
        All that can be done with a c++ model and a view (ListView, Repeater, ...) with delegates in QML.

        I would also recommend not to use Qt.createQmlObject or Qt.createComponent from your link (too imperative) and stay on the declarative side by using models and views.

        S Offline
        S Offline
        spiderdab
        wrote on last edited by
        #3

        @GrecKo thanks.
        Will , in this way, the c++ variables (model?) be visible inside main.cpp?

        I'm looking here and here to understand your suggestion.

        1 Reply Last reply
        0
        • GrecKoG Offline
          GrecKoG Offline
          GrecKo
          Qt Champions 2018
          wrote on last edited by
          #4

          Yes it can, follow your second link if you want that.

          1 Reply Last reply
          1
          • S Offline
            S Offline
            spiderdab
            wrote on last edited by
            #5

            Thank you. I'll go on that way.

            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