How to write a QML extension from a custom C++ class?
-
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,
.pro
and .h
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. -
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.
-
Have you looked at this example in the documentation.
General steps...
- Inherit class from OQObject
- Register you class with qmlRegisterType(...)
- import the package in qml(main.qml)
-
From a quick glance at your code it should work, have you tried running it and ignoring the Qt Creator warning?
-
@dheerendra My class inherits from QWebEnginePage which inherits from QObject, so normally it also indirectly inherits from QObject
-
Show me where did you register you class ? I don't see an import statement at the top as well.
-
@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
andCONFIG += qmltypes QML_IMPORT_NAME = QtWebView QML_IMPORT_MAJOR_VERSION = 1
There's
import QtWebView 1.0
at the top of the main.qml file -
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.