通过反复试验,我发现如果要从QML调用C++类的虚函数,则必须在类层次结构中将它们全部声明为Q_INVOKABLE,否则会得到类型错误:



我真的找不到与此相关的任何文档,请有人指出我的意思吗?如果我想注册一个派生的类类型并在QML中实例化,该怎么办?将虚拟函数声明为Q_INVOKABLE是否有性能方面的问题?此刻,我正在做的事情(原型(prototype))。

class B : public QObject
{
public:
   virtual void foo();
}

class D : public B
{
public:
   Q_INVOKABLE virtual void foo();
}

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    Base * derived = new D;

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

   engine.rootContext()->setContextProperty("derived", d);

    return app.exec();
}

最佳答案

您可以在Q_INVOKABLE部分下声明您的函数,而不是使用slots:

class D : public B
{
    Q_OBJECT
public slots:
    virtual void foo();
}

查看此链接以获取详细说明:

http://developer.nokia.com/community/wiki/Calling_Qt_class_methods_from_QML

另外,不要忘记您的类中的Q_OBJECT宏。

更新
Q_INVOKABLEslots关键字基本上都执行相同的操作。他们向Qt元系统“注册”您的功能。正如您在注释中指出的,区别在于,使用Q_INVOKABLE可以返回值。

在虚拟函数方面,它们透明地工作,就像在常规C++中一样。

在您的示例中,由于您将Base*指针传递给QML,因此需要通过添加Q_OBJECT宏为该类“启用” Qt元系统,并且需要向foo注册其Q_INVOKABLE函数。

您不必在派生类中这样做。因此您的代码将是:
class B : public QObject
{
    Q_OBJECT
public:
    Q_INVOKABLE virtual void foo();
}

class D : public B
{
public:
    virtual void foo();
}

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    Base * derived = new D;

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    engine.rootContext()->setContextProperty("derived", d);

    return app.exec();
}

以下是我根据以上链接文章修改的完整示例。它使用Q_INVOKABLEslotsvirtual函数。

example.pro
QT += declarative

HEADERS += stringhelper.h
SOURCES += main.cpp

stringhelper.h
#ifndef STRINGHELPER_H
#define STRINGHELPER_H

#include <QObject>
#include <QString>

// outputs text, ignoring reverse
class StringHelper_Base : public QObject
{
    Q_OBJECT
public:
    StringHelper_Base(QObject *parent = 0): QObject(parent) { }

    Q_INVOKABLE virtual QString echo(const QString &text) const {
        return text;
    }

public slots:
    virtual void toggleEcho(bool reverse) { (void)reverse; }
};

// outputs text with reversing
class StringHelper : public StringHelper_Base
{
public:
    StringHelper(QObject *parent = 0): StringHelper_Base(parent), reverse(false) { }

    QString echo(const QString &text) const {
        if(reverse == false) { return text; }

        QString reversed;
        for(QString::const_iterator it = text.begin(); it != text.end(); it++) {
            reversed.push_front(*it);
        }

        return reversed;
    }

    void toggleEcho(bool reverse) { this->reverse = reverse; }

protected:
    bool reverse;
};

#endif // STRINGHELPER_H

main.cpp
#include <QApplication>
#include <QDeclarativeView>
#include <QDeclarativeContext>
#include "stringhelper.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    //StringHelper_Base* stringHelper = new StringHelper_Base();
    StringHelper_Base* stringHelper = new StringHelper();

    QDeclarativeView view;
    view.setResizeMode(QDeclarativeView::SizeRootObjectToView);
    view.rootContext()->setContextProperty("foo", stringHelper);
    view.setSource(QUrl("./ui.qml"));

    view.setGeometry(100, 100, 800, 480);
    view.show();

    return a.exec();
}

ui.qml
import QtQuick 1.1

Rectangle {
    id: rect

    property string text: "Using Qt class to echo this"

    function updateUI() {
        foo.toggleEcho(button.pressed); // calling StringHelper::toggleEcho
        text.text = foo.echo(rect.text) // calling StringHelper::echo
    }

    anchors.fill: parent
    color: "black"

    Component.onCompleted: updateUI()

    Text {
        id: text
        anchors.centerIn: parent
        color: "white"
    }

    Rectangle {
        id: button

        property bool pressed: false

        width: 100; height: 40
        anchors.right: parent.right; anchors.rightMargin: 20
        anchors.bottom: parent.bottom; anchors.bottomMargin: 20
        radius: 6
        color: pressed ? "gray" : "white"

        Text {
            anchors.centerIn: parent
            text: "Reverse"
        }

        MouseArea {
            anchors.fill: parent
            onClicked: { button.pressed = !button.pressed; updateUI() }
        }
    }
}

10-07 19:17
查看更多