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 to write a QML extension from a custom C++ class?
QtWS25 Last Chance

How to write a QML extension from a custom C++ class?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qtquickc++ qtextension
7 Posts 3 Posters 1.1k 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.
  • G Offline
    G Offline
    gouneken
    wrote on last edited by
    #1

    Good evening to the whole community, I am working on a mobile application project for search engine and for this I use the C++ and QML languages. I would like to extend my C++ class into a QML element. I tried to follow the procedure on the documentation, but at home it does not work as expected because when I implement my class in the main.qml file, I get the error message "Unknown component (M300)".
    Here are some screenshots of my main.qml,
    a4783c0e-cac7-4498-bcf3-522c72f5240b-image.png
    .pro
    6c52a92c-491b-45b6-a1e9-5cf36fcdaf97-image.png
    and .h
    efc4ad0d-8550-4892-a66c-199df9e6c54d-image.png
    I am a beginner on Qt and I would like to know what my mistake is and how should I have done it. Thank you very much and I look forward to your answers.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      gouneken
      wrote on last edited by
      #7

      when I rerun, I am told that QtWebView cannot contain more than one item, so I guess I cannot save a c++ class in QtWebView, going through the doc I found that the c++ classes I needed had already been extended to the quick module. So, I'm going to use the c++ classes in this module instead. Thank you very much for your help.

      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #2

        Have you looked at this example in the documentation.

        General steps...

        1. Inherit class from OQObject
        2. Register you class with qmlRegisterType(...)
        3. import the package in qml(main.qml)

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

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

          From a quick glance at your code it should work, have you tried running it and ignoring the Qt Creator warning?

          1 Reply Last reply
          0
          • dheerendraD dheerendra

            Have you looked at this example in the documentation.

            General steps...

            1. Inherit class from OQObject
            2. Register you class with qmlRegisterType(...)
            3. import the package in qml(main.qml)
            G Offline
            G Offline
            gouneken
            wrote on last edited by
            #4

            @dheerendra My class inherits from QWebEnginePage which inherits from QObject, so normally it also indirectly inherits from QObject

            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #5

              Show me where did you register you class ? I don't see an import statement at the top as well.

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              GrecKoG 1 Reply Last reply
              0
              • dheerendraD dheerendra

                Show me where did you register you class ? I don't see an import statement at the top as well.

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

                @dheerendra said in How to write a QML extension from a custom C++ class?:

                Show me where did you register you class ? I don't see an import statement at the top as well.

                The registration is done with QML_ELEMENT and

                CONFIG += qmltypes
                QML_IMPORT_NAME = QtWebView
                QML_IMPORT_MAJOR_VERSION = 1
                

                There's import QtWebView 1.0 at the top of the main.qml file

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  gouneken
                  wrote on last edited by
                  #7

                  when I rerun, I am told that QtWebView cannot contain more than one item, so I guess I cannot save a c++ class in QtWebView, going through the doc I found that the c++ classes I needed had already been extended to the quick module. So, I'm going to use the c++ classes in this module instead. Thank you very much for your help.

                  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