Getting C++ struct in QML as type
-
I am on Qt 6.8. I am able to get the struct data in the QML but I am not able to get it as a type. The application name is
DataLogStream
and here is my struct:#ifndef SERIALPORTSETTINGS_H #define SERIALPORTSETTINGS_H #include <QObject> #include <QSerialPort> #include <QMetaType> #include <QQmlEngine> struct SerialPortSettings { Q_GADGET QML_VALUE_TYPE(serialPortSettings) Q_PROPERTY(QString name MEMBER name) Q_PROPERTY(qint32 baudRate MEMBER baudRate) Q_PROPERTY(QString stringBaudRate MEMBER stringBaudRate) Q_PROPERTY(QSerialPort::DataBits dataBits MEMBER dataBits) Q_PROPERTY(QString stringDataBits MEMBER stringDataBits) Q_PROPERTY(QSerialPort::Parity parity MEMBER parity) Q_PROPERTY(QString stringParity MEMBER stringParity) Q_PROPERTY(QSerialPort::StopBits stopBits MEMBER stopBits) Q_PROPERTY(QString stringStopBits MEMBER stringStopBits) Q_PROPERTY(QSerialPort::FlowControl flowControl MEMBER flowControl) Q_PROPERTY(QString stringFlowControl MEMBER stringFlowControl) Q_PROPERTY(bool localEchoEnabled MEMBER localEchoEnabled) Q_PROPERTY(QString delimiter MEMBER delimiter) Q_PROPERTY(QString samplingFrequency MEMBER samplingFrequency) Q_PROPERTY(QString headerLength MEMBER headerLength) Q_PROPERTY(bool mergePlots MEMBER mergePlots) public: QString name; qint32 baudRate; QString stringBaudRate; QSerialPort::DataBits dataBits; QString stringDataBits; QSerialPort::Parity parity; QString stringParity; QSerialPort::StopBits stopBits; QString stringStopBits; QSerialPort::FlowControl flowControl; QString stringFlowControl; bool localEchoEnabled; QString delimiter; QString samplingFrequency; QString headerLength; bool mergePlots; }; Q_DECLARE_METATYPE(SerialPortSettings) #endif // SERIALPORTSETTINGS_H
I then added this to
SOURCE
underqt_add_qml_module
. I haveportselectionservice.cpp/.h
which is aQML_ELEMENT
, which has all the setter and getter for thisSerialPortSettings
. Theportselectionservice.cpp/.h
can be called throughPortSelection.qml
by its type after importingDataLogStream
in QML. And on a click of a button, the struct data is sent fromPortSelection.qml
toPlotStream.qml
.And I am trying to use this type as a property with:
import DataLogStream ApplicationWindow { width: 1080 height: 720 property SerialPortSettings portSelectionSettings // .... }
Now when I try to use
SerialPortSettings
orserialPortSettings
as a property type, I am getting an errorSerialPortSettings is not a type
same forserialPortSettings
. But if I just use it asvar
-property var portSelectionSettings
- I am able to see the data.Here is the
DataLogStream.qmltypes
import QtQuick.tooling 1.2 // This file describes the plugin-supplied types contained in the library. // It is used for QML tooling purposes only. // // This file was auto-generated by qmltyperegistrar. Module { Component { file: "plotstreamservice.h" name: "PlotStreamService" accessSemantics: "reference" prototype: "QObject" exports: ["DataLogStream/PlotStreamService 0.0"] exportMetaObjectRevisions: [0] Property { ...} } Component { file: "portselectionservice.h" name: "PortSelectionService" accessSemantics: "reference" prototype: "QObject" exports: ["DataLogStream/PortSelectionService 0.0"] exportMetaObjectRevisions: [0] Property { ...} Method { name: "start" } } Component { file: "serialportsettings.h" name: "SerialPortSettings" accessSemantics: "value" exports: ["DataLogStream/serialPortSettings 0.0"] isCreatable: false exportMetaObjectRevisions: [0] Property { ... } } }
The
SerialPortSettings
exists but the typesystem can't seem to catch it. How should I use the proper type here without causing an error? -
I think you missed one of the requirements, see https://doc.qt.io/qt-6/qtqml-cppintegration-definetypes.html#registering-value-types
"In contrast to object types, value types require lower case names." -
Somewhere in your build folder, you should be able to find a generated file containing the QML type registrations. They end with "_qmltyperegistrations.cpp", and should start with the name of the QML module.
Please check if your type is registered there.
If not, the problem is on the C++ or possibly cmake side
If it is, more likely on the user (QML) side -
Also, do you import the QML module by the correct name in QML?
-
@Asperamanca said in Getting C++ struct in QML as type:
Somewhere in your build folder, you should be able to find a generated file containing the QML type registrations. They end with "_qmltyperegistrations.cpp", and should start with the name of the QML module.
Please check if your type is registered there.
If not, the problem is on the C++ or possibly cmake side
If it is, more likely on the user (QML) sideYes, it did create it and here is the output
/**************************************************************************** ** Generated QML type registration code ** ** WARNING! All changes made in this file will be lost! *****************************************************************************/ #include <QtQml/qqml.h> #include <QtQml/qqmlmoduleregistration.h> #if __has_include(<plotstreamservice.h>) # include <plotstreamservice.h> #endif #if __has_include(<portselectionservice.h>) # include <portselectionservice.h> #endif #if __has_include(<serialportsettings.h>) # include <serialportsettings.h> #endif #if !defined(QT_STATIC) #define Q_QMLTYPE_EXPORT Q_DECL_EXPORT #else #define Q_QMLTYPE_EXPORT #endif Q_QMLTYPE_EXPORT void qml_register_types_DataLogStream() { qmlRegisterModule("DataLogStream", 0, 0); QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED qmlRegisterTypesAndRevisions<PlotStreamService>("DataLogStream", 0); qmlRegisterTypesAndRevisions<PortSelectionService>("DataLogStream", 0); qmlRegisterTypesAndRevisions<SerialPortSettings>("DataLogStream", 0); QT_WARNING_POP qmlRegisterModule("DataLogStream", 0, 1); } static const QQmlModuleRegistration dataLogStreamRegistration("DataLogStream", qml_register_types_DataLogStream);
Also, do you import the QML module by the correct name in QML?
Yes, I did. i have been using
import DataLogStream
-
I think you missed one of the requirements, see https://doc.qt.io/qt-6/qtqml-cppintegration-definetypes.html#registering-value-types
"In contrast to object types, value types require lower case names." -
I did -
QML_VALUE_TYPE(serialPortSettings)
- isn't this the way? Or do you mean it should be completely be in lower cases? -
You are right. I mis-read the requirements to apply to the C++ type name.
-
@Asperamanca I tried to change the whole text to lowercase and that worked!
-