最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Getting C++ struct in QML as type - Stack Overflow

programmeradmin2浏览0评论

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.

Share Improve this question edited Mar 11 at 20:44 Akshay asked Mar 11 at 11:33 AkshayAkshay 2,8055 gold badges41 silver badges77 bronze badges 8
  • Based on your description, I would expect that property serialPortSettings portSelectionSettings is correct - the name of the type should be the token inside QML_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:52
  • It contains SerialPortSettings component, I have updated the question. – Akshay Commented Mar 11 at 20:45
  • Why haven't you gone with QObject? – Stephen Quan Commented Mar 11 at 22:50
  • I didn’t really need the whole functionality of QObject and the document suggests Q_GADGET for structs – Akshay Commented Mar 12 at 6:46
  • This does clarify that my initial guess should be right: exports: ["DataLogStream/serialPortSettings 0.0"] - because the import is DataLogStream/serialPortSettings, it confirms that you should be able to use import DataLogStream and then the type name serialPortSettings like you are – Ipiano Commented Mar 12 at 18:45
 |  Show 3 more comments

1 Answer 1

Reset to default 0

I 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
}
发布评论

评论列表(0)

  1. 暂无评论