How do you pass a pointer to string property from QML to C++?
When I attempted to do it the obvious way, I got this error:
qrc:/NewAccount.qml:236: Error: Unknown method parameter type: QString*
Which means, QML engine can't convert new_account.error_string
property to C++ when calling save_account
(Q_INVOKABLE) method
This is my code in QML:
import myproject.aewm.ethkey 1.0
import myproject.aewm.walletaccount 1.0
...
id: new_account
property EthKey key: EthKey{}
property WalletAccount account: WalletAccount{}
property string error_string: ""
....
if (aewm_obj.save_account(key,account,new_account.error_string)) {
wallet_accounts_tabbar.currentIndex=0
} else {
console.log("error occurred:"+new_account.error_string)
}
Where aewm_obj
is a C++ type (registered in QML) and save_account
is declared in C++ as :
Q_INVOKABLE bool save_account(EthKey *key, WalletAccount *account, QString *err_str);
The docs say that a (QML) string
property is a QString
object, and that these types are automatically converted from from C++ and QML . I am passing pointers to my custom QObject-derived classes without any problems after qmlRegisterType()
call, so why I can't do the same with strings?
I thought that maybe string pointers are not supported and I tried to add this line:
qRegisterMetaType<QString*>("QString*");
but after this change, the pointer I received at C++ side was 0x0 and I got a segfault.
So, how do you pass pointers to QML string properties from QML to C++ ?
Or, do you think I should register QString
class with qmlRegisterType() too? I tried it but I have encountered some compilation issues, so I think it won't compile.
The last solution would be to create a custom object with a QString inside, and send a pointer to it from QML to C++. But, this would be an overkill, if QString exists why not find a way to use it?
Will appreciate very much your comments. I definitely want to use pointers , it is safer than dealing with object ownership when exchanging data between C++ and QML.
qmlRegisterType()
on an class, you can pass a pointer to its objects from QML to C++ without any problem. I am doing this all the time. But it only works with my custom classes, it doesn't work withQString
. But why ? Is there a way to makeQString
pointers to work like other class pointers? – NulikqmlRegisterType()
works only withQObject
subclasses, andQString
ain't that. And it is only so that you can create the objects declaratively. RegularQObject
s can be perfectly accessed from QML without registering. AndQString
is already automatically converted to astring
on the QML side. SupportingQObject
s via pointers is just an auxiliary, it doesn't mean QML supports pointers, it does not. – dtech