I am afraid you can't use std::string
directly within QML, you have to convert to QString
first.
In general, you can only export two kinds of C++ types to QML: QObject-derived classes and some build-in value types, see this documentation page. So using a QString
as the type of a Q_PROPERTY only works because it has special built-in support in the QML engine. There is no way to add support for more of these value types like std::string
(*).
Using qRegisterMetaType
also doesn't help: It merely registers std::string
as a meta type, which among others means it can be stored in a QVariant
. It is (mostly) not relevant for QML integration.
So have your getter return a QString
:
Q_PROPERTY(QString stldata READ stldata WRITE setSTLData NOTIFY stldataChanged)
QString stldata() const {
return QString::fromStdString(myStlString);
}
(*) Actually there is a way to add support for new value types, but that is private API in QML that needs to work directly with the underlying Javascript engine. The QtQuick module uses that private API to add support for e.g. the QMatrix4x4
value type.