Qt5 Qtcreator 使用 main.qml 中的 c++ 函数创建文件 /tmp/

sUb*_*ack 5 application-development qml qt5 ubuntu-touch

我想基于 QtQuick2ApplicationViewer 模板为 Ubuntu Touch 编写一个应用程序,并且该应用程序需要写入一个文件(例如“/tmp/test.log”)。

我使用函数/宏进行了测试Q_INVOKABLE

Q_INVOKABLE void QtQuick2ApplicationViewer::setData3(void) {

    qDebug() << "setData";

}
Run Code Online (Sandbox Code Playgroud)

...并分享给Q_INVOKABLE.

但是我无法在我的 .qml 中调用该函数,并且出现错误:

...main.qml:289: ReferenceError: setData3 "is not defined"
Run Code Online (Sandbox Code Playgroud)

有没有人要下载一个有效的 demo.pro 文件?

或者使用 cpp-function+qml 作为 .pro 创建文件的工作方法进行下载?

在 ubuntutouch 的 qtcreator 版本中工作?

sUb*_*ack 4

我自己找到了解决方案,我想解释一下我做错了什么。

我忘了在:

主程序

    #include <QQmlEngine>
    #include <QQmlComponent>
    #include "stringhelper.h"
   to 
    qmlRegisterType<StringHelper>("MyStringHelper", 1, 0, "StringHelper");
Run Code Online (Sandbox Code Playgroud)

字符串助手.h:

    ...class...
    .....public slots:...
           Q_INVOKABLE void stringxx(void){
              qDebug() << "  stringhelper.h:stringxx GOT YOU !! :-) ";
           }
Run Code Online (Sandbox Code Playgroud)

现在在 main.qml 中:

    import MyStringHelper 1.0

    StringHelper {
            id: my_StringH
    }

    my_StringH.stringxx();
Run Code Online (Sandbox Code Playgroud)

现在我可以在 .qml 中调用我的 stringxx 函数和更强大的函数

问候萨沙