Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Create a method for a custom type for QML created by QT c++
QtWS25 Last Chance

Create a method for a custom type for QML created by QT c++

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 776 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.
  • carles.sole.grauC Offline
    carles.sole.grauC Offline
    carles.sole.grau
    wrote on last edited by carles.sole.grau
    #1

    Hi everybody,
    I'm trying to add a new custom type to the QML.
    Everything works fine except I don't know how to add its own functions to the new type created:

    My .h file is:

    #ifndef SERIALCOMMUNICATION_H
    #define SERIALCOMMUNICATION_H
    
    #include <QObject>
    #include <QSerialPort>
    #include <QByteArray>
    #include <QtQuick>
    
    class SerialCommunicationtoQML: public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
        Q_PROPERTY(int numAvailablePorts READ numAvailablePorts WRITE setnumAvailablePorts NOTIFY numAvailablePortsChanged)    
    
    public:
    
        SerialCommunicationtoQML(QQuickItem *parent = 0);
    
        QString name() const;
        void setName(const QString &name);
    
        int numAvailablePorts() const;
        void setnumAvailablePorts(const int &numAvailablePorts);
    
        void getPortInfo();
    
    signals:
    
        void nameChanged();
        void numAvailablePortsChanged();
    
    private:
    
        QString m_name;
        int m_numAvailablePorts;
    
    };
    
    #endif // SERIALCOMMUNICATION_H
    

    Then my .cpp file is:

    #include "serialcommunication.h"
    
    
    SerialCommunicationtoQML::SerialCommunicationtoQML(QQuickItem *parent)    : QObject(parent)
    {
        //Aquí podem inicialitzar les variables amb el seu valor per defecte
        m_numAvailablePorts = 0;
    }
    
    QString SerialCommunicationtoQML::name() const
    {
        return m_name;
    }
    
    void SerialCommunicationtoQML::setName(const QString &name)
    {
        if (name != m_name) {
            m_name = name;
            emit nameChanged();
        }
    }
    
    int SerialCommunicationtoQML::numAvailablePorts() const
    {
        return m_numAvailablePorts;
    }
    
    void SerialCommunicationtoQML::setnumAvailablePorts(const int &numAvailablePorts)
    {
        if (numAvailablePorts != m_numAvailablePorts) {
            m_numAvailablePorts = numAvailablePorts;
            emit numAvailablePortsChanged();
        }
    }
    
    void SerialCommunicationtoQML::getPortInfo()
    {
        
    }
    

    And finally my .qml file (for testing ) is:

    import QtQuick 2.4
    import QtQuick.Window 2.2
    import SerialCommunication.toQML 1.0
    
    
    Window {
        visible: true    
        height: 700
        width: 700
    
        Rectangle {
            color: "lightblue"
            anchors.centerIn: parent
            width: parent.width/2
            height: parent.width/2
    
            Text {
                anchors.centerIn: parent
                text:"El nom és " + serial.name + "\nNúm de ports disponibles: " + serial.numAvailablePorts
            }
    
            SerialCommunication {
                id: serial
                name: "INITIAL NAME"
                
            }
    
        }
        MouseArea{
            anchors.fill: parent
            onClicked: {
                console.log("CLICKED ")
                serial.getPortInfo()
            }
    
        }
    }
    

    So when I press the mouse area, I expect to go to the getPortInfo function but:

    qrc:/main.qml:33: TypeError: Property 'getPortInfo' of object SerialCommunicationtoQML(0x16c5da0) is not a function
    

    Thank you!

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi! The functions that shall be callable from QtQuick must be declared as either slot or invokable, e.g.:

      public: 
      Q_INVOKABLE void getPortInfo();
      
      1 Reply Last reply
      1
      • carles.sole.grauC Offline
        carles.sole.grauC Offline
        carles.sole.grau
        wrote on last edited by
        #3

        Thank you very much @Wieland ,
        Now it's working!

        1 Reply Last reply
        0

        • Login

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