Qt5 Qtcreator在main.qml中使用c ++函数创建文件/ tmp /
我想基于QtQuick2ApplicationViewer模板为Ubuntu Touch编写应用程序,并且应用程序需要写入文件(例如’/tmp/test.log’)。
我使用函数/宏Q_INVOKABLE
进行了测试:
Q_INVOKABLE void QtQuick2ApplicationViewer::setData3(void) { qDebug() << "setData"; }
……并通过Q_INVOKABLE
分享。
但我无法在我的.qml中调用该函数,并且出现错误:
...main.qml:289: ReferenceError: setData3 "is not defined"
有没有人有一个工作的demo.pro文件下载?
或者使用cpp-function + qml创建文件的工作方法如下载?
在utntutouch的qtcreator版本中工作?
我自己找到了解决方案,我想解释一下我做错了什么。
我忘记了:
main.c中
#include #include #include "stringhelper.h" to qmlRegisterType("MyStringHelper", 1, 0, "StringHelper");
stringhelper.h:
...class... .....public slots:... Q_INVOKABLE void stringxx(void){ qDebug() << " stringhelper.h:stringxx GOT YOU !! :-) "; }
现在在main.qml中:
import MyStringHelper 1.0 StringHelper { id: my_StringH } my_StringH.stringxx();
现在我可以调用我的stringxx函数和更强大的函数im .qml
关于Sascha