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
under qt_add_qml_module
. I have portselectionservice.cpp/.h
which is a QML_ELEMENT
, which has all the setter and getter for this SerialPortSettings
. The portselectionservice.cpp/.h
can be called through PortSelection.qml
by its type after importing DataLogStream
in QML. And on a click of a button, the struct data is sent from PortSelection.qml
to PlotStream.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
or serialPortSettings
as a property type, I am getting an error SerialPortSettings is not a type
same for serialPortSettings
. But if I just use it as var
- property var portSelectionSettings
- I am able to see the data.
How should I use the proper type here without causing this error?
Update
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.
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
under qt_add_qml_module
. I have portselectionservice.cpp/.h
which is a QML_ELEMENT
, which has all the setter and getter for this SerialPortSettings
. The portselectionservice.cpp/.h
can be called through PortSelection.qml
by its type after importing DataLogStream
in QML. And on a click of a button, the struct data is sent from PortSelection.qml
to PlotStream.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
or serialPortSettings
as a property type, I am getting an error SerialPortSettings is not a type
same for serialPortSettings
. But if I just use it as var
- property var portSelectionSettings
- I am able to see the data.
How should I use the proper type here without causing this error?
Update
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.
1 Answer
Reset to default 0I think I figured it out. Although the starting letter in this line - QML_VALUE_TYPE(serialPortSettings)
is in lower case, the complete text should be in lower case - QML_VALUE_TYPE(serialportsettings)
. Here is the documentation for that - https://doc.qt.io/qt-6/qtqml-cppintegration-definetypes.html#registering-value-types
So the code becomes:
struct SerialPortSettings {
Q_GADGET
QML_VALUE_TYPE(serialportsettings)
...
}
I can now use something like:
import QtQuick
import DataLogStream
Item {
property serialportsettings serialportsettings
}
property serialPortSettings portSelectionSettings
is correct - the name of the type should be the token insideQML_VALUE_TYPE
. Take a look at the generated QML type file - you should be able to find a.qmltypes
file in the build folder that defines the C++-QML translations for your QML module. If your type is there, but named differently, or not there, that may provide some insight. It could also be an issue related to using the SerialPort enum types - QML compiler may not know how to deal with them - you could try just using ints – Ipiano Commented Mar 11 at 16:52SerialPortSettings
component, I have updated the question. – Akshay Commented Mar 11 at 20:45exports: ["DataLogStream/serialPortSettings 0.0"]
- because the import isDataLogStream/serialPortSettings
, it confirms that you should be able to useimport DataLogStream
and then the type nameserialPortSettings
like you are – Ipiano Commented Mar 12 at 18:45