everyone. I apologize in advance for my English.
Could someone help me? I don’t understand why this is happening and what I’m doing wrong.
I have a separate class with static constants (currently string constants)
namespace constants {
Q_NAMESPACE
class SharedConstants : public QObject {
Q_OBJECT
Q_PROPERTY(QString Property MEMBER Property CONSTANT)
public:
static inline const QString Property{"MY_PROPERTY_TEXT"};
};
} // namespace constants
In main.cpp, I expose it to QML using
qmlRegisterUncreatableType<constants::SharedConstants>(
"Constants", 1, 0, "SharedConstants", "SharedConstants class is not createable");
And when I try to access my property in QML using
import Constants 1.0
...
console.info(SharedConstants.Property)
I get the output undefined
.
I assume that the static class parameters were not somehow defined in QML, but I don’t understand why.
Initially, I tried to simply expose constants to QML, assuming that I need to use import Constants
. But I wasn’t successful and decided to use the class as an auxiliary tool. However, even here, something is not working for me. Please help. If you know how to expose constants from a namespace to QML using import
, I would also appreciate it.
I’m using Qt 5.15.2, C++17 standard.
Илья Ковалев is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.