本文介绍了C++ 类以时尚 TypeError 暴露于 QML 错误:对象的属性“..."不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地向 QML 公开了一个 C++ 类.它在 Qt Creator 中注册并找到.其目的是连接数据库,如下代码所示:

I've successfully exposed a C++ class to QML. It is registered and found in Qt Creator. It's purpose is to connect to a database, as shown in following code:

#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

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

However, when I try to call method isConnected() from the following QML code

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

我收到以下错误:

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

我做错了什么?

推荐答案

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

You have this error because you have declared the property isConnected in C++ but you're calling it from QML in the wrong way: uePosDatabase.isConnected is the correct way, not uePosDatabase.isConnected().

如果你想调用函数isConnected(),你应该改变它的名字来区别于属性,比如getIsConnected().鉴于您的属性声明,您既不需要直接调用此函数,也不需要使用 Q_INVOKABLE 宏从 QML 调用它.

If you want to call the function isConnected() you should change its name to differate it from the property, like getIsConnected(). Given your property declaration, you neither need to call this function directly nor you need to make it callable from QML with the Q_INVOKABLE macro.

这篇关于C++ 类以时尚 TypeError 暴露于 QML 错误:对象的属性“..."不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 01:21