Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Getting C++ struct in QML as type

Getting C++ struct in QML as type

Scheduled Pinned Locked Moved Solved General and Desktop
qmlc++struct
8 Posts 2 Posters 271 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.
  • A Offline
    A Offline
    akshaybabloo
    wrote on 12 Mar 2025, 08:04 last edited by
    #1

    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 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.

    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?

    1 Reply Last reply
    1
    • A Offline
      A Offline
      Asperamanca
      wrote on 13 Mar 2025, 15:12 last edited by
      #5

      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."

      1 Reply Last reply
      1
      • A Offline
        A Offline
        Asperamanca
        wrote on 12 Mar 2025, 10:06 last edited by Asperamanca 3 Dec 2025, 10:06
        #2

        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

        A 1 Reply Last reply 12 Mar 2025, 21:08
        0
        • A Offline
          A Offline
          Asperamanca
          wrote on 12 Mar 2025, 10:07 last edited by
          #3

          Also, do you import the QML module by the correct name in QML?

          1 Reply Last reply
          0
          • A Asperamanca
            12 Mar 2025, 10:06

            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

            A Offline
            A Offline
            akshaybabloo
            wrote on 12 Mar 2025, 21:08 last edited by
            #4

            @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) side

            Yes, 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

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Asperamanca
              wrote on 13 Mar 2025, 15:12 last edited by
              #5

              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."

              1 Reply Last reply
              1
              • A Offline
                A Offline
                akshaybabloo
                wrote on 16 Mar 2025, 06:07 last edited by
                #6

                I did - QML_VALUE_TYPE(serialPortSettings) - isn't this the way? Or do you mean it should be completely be in lower cases?

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Asperamanca
                  wrote on 17 Mar 2025, 10:10 last edited by
                  #7

                  You are right. I mis-read the requirements to apply to the C++ type name.

                  A 1 Reply Last reply 18 Mar 2025, 02:32
                  0
                  • A Asperamanca
                    17 Mar 2025, 10:10

                    You are right. I mis-read the requirements to apply to the C++ type name.

                    A Offline
                    A Offline
                    akshaybabloo
                    wrote on 18 Mar 2025, 02:32 last edited by
                    #8

                    @Asperamanca I tried to change the whole text to lowercase and that worked!

                    1 Reply Last reply
                    0
                    • A akshaybabloo has marked this topic as solved on 18 Mar 2025, 02:33

                    6/8

                    16 Mar 2025, 06:07

                    • Login

                    • Login or register to search.
                    6 out of 8
                    • First post
                      6/8
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved