C++类暴露于时尚中的QML错误TypeError:对象的属性'...'不是函数

Ker*_*nic 4 qt qml qtquick2

我已成功地将C++类暴露给QML.它已在Qt Creator中注册并找到.它的目的是连接到数据库,如下面的代码所示:

#ifndef UESQLDATABASE_H
#define UESQLDATABASE_H

#include <QObject>
#include <QtSql/QSqlDatabase>

class UeSqlDatabase : public QObject
{
    Q_OBJECT

    Q_PROPERTY(bool m_ueConnected READ isConnected WRITE setConnected NOTIFY ueConnectedChanged)

private:
    bool m_ueConneted;

    inline void setConnected(const bool& ueConnected)
        { this->m_ueConneted=ueConnected; }

public:
    explicit UeSqlDatabase(QObject *parent = 0);

    Q_INVOKABLE inline const bool& isConnected() const
        { return this->m_ueConneted; }

    ~UeSqlDatabase();

signals:
    void ueConnectedChanged();

public slots:
    void ueConnectToDatabase (const QString& ueStrHost, const QString& ueStrDatabase,
                              const QString& ueStrUsername, const QString& ueStrPassword);
};

#endif // UESQLDATABASE_H
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试isConnected()从以下QML代码调用方法时

import QtQuick 2.0

Rectangle
{
    id: ueMenuButton

    property string ueText;

    width: 192
    height: 64
    radius: 8

    states: [
        State
        {
            name: "ueStateSelected"

            PropertyChanges
            {
                target: gradientStop1
                color: "#000000"
            }

            PropertyChanges
            {
                target: gradientStop2
                color: "#3fe400"
            }
        }
    ]

    gradient: Gradient
    {
        GradientStop
        {
            id: gradientStop1
            position: 0
            color: "#000000"
        }

        GradientStop
        {
            position: 0.741
            color: "#363636"
        }

        GradientStop
        {
            id: gradientStop2
            position: 1
            color: "#868686"
        }

    }

    border.color: "#ffffff"
    border.width: 2
    antialiasing: true

    Text
    {
        id: ueButtonText
        color: "#ffffff"
        text: qsTr(ueText)
        clip: false
        z: 0
        scale: 1
        rotation: 0
        font.strikeout: false
        anchors.fill: parent
        font.bold: true
        style: Text.Outline
        textFormat: Text.RichText
        verticalAlignment: Text.AlignVCenter
        horizontalAlignment: Text.AlignHCenter
        font.pixelSize: 16
}

    MouseArea
    {
        id: ueClickArea

        antialiasing: true
        anchors.fill: parent

        onClicked:
        {
            uePosDatabase.ueConnectToDatabase("127.0.0.1",
                                              "testDb",
                                              "testUser",
                                              "testPassword");
            if(uePosDatabase.isConnected()==true)
            {
                ueMenuButton.state="ueStateSelected";
            }
            else
            {
                ueMenuButton.state="base state"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

qrc:/UeMenuButton.qml:92:TypeError:对象的属性'isConnected'UeSqlDatabase(0x1772060)不是函数

我究竟做错了什么?

Mid*_*ido 6

你有这个错误,因为你已经isConnected在C++中声明了属性,但你是以错误的方式从QML调用它:uePosDatabase.isConnected是正确的方法,而不是uePosDatabase.isConnected().

如果要调用该函数,isConnected()您应该更改其名称以使其与属性不同,例如getIsConnected().鉴于您的属性声明,您既不需要直接调用此函数也不需要QML使用Q_INVOKABLE宏来调用它.