QGuiApplication app(argc, argv);
QQmlEngine engine;
QQmlContext* maincontext = new QQmlContext(engine.rootContext());
MainWindowMC mainMC(maincontext);
QQmlComponent component(&engine, QUrl::fromLocalFile("qml/convQML/main.qml"));
QQuickWindow* window = qobject_cast<QQuickWindow*> (component.create(maincontext));
window->show();
return app.exec();
This is my main.cpp.
I pass pointer of context to MainWindowMC.
Here is MainWindowMC:
MainWindowMC::MainWindowMC(QQmlContext* maincontext)
:MainM()
{
typeGenerator();
maincontext->setContextProperty("mainwin", this);
maincontext->setContextProperty("typemodel", QVariant::fromValue(m_typemodel));
maincontext->setContextProperty("unitmodel", QVariant::fromValue(m_unitmodel));
}
and the class declaration:
public:
MainWindowMC(QQmlContext*);
QStringList typemodel();
void typeGenerator();
Q_INVOKABLE void unitGenerator(QString type);
It seems I have done everything – registered object in context, made Q_INVOKABLE function-but I cannot call the funtion from QML. It throws “TypeError: Object [object Object] has no method ‘unitGenerator’”
What am I doing wrong?
Optionaly, this is how I try to use the function in QML:
onCurrentTextChanged: mainwin.unitGenerator(typebox.currentText)
↧