PDA

View Full Version : Error calling C++ function from QML



rhb327
15th January 2020, 00:39
Not quite sure where my error is. I wanted to share an enum and class to Qt...I read that qmlRegisterUncreatableMetaObject does not import properly and experienced that issue myself so the below is what I came up with by I'm receiving an error:

qrc:/Pages/AddCustomFields.qml:288: Error: Unknown method parameter type: OpsMode::ModeCRF

Any insight appreciated!

Thanks,
-Rich

---------------8<--------------------------------------------
Header:

class OpsMode : public QObject {
Q_OBJECT

public:
enum ModeCRF : int {ModeIdle = 0, ModeActiveRun, ModeWarming, ModeRemote};
Q_ENUM(ModeCRF)
};

class OperationMode : public QObject
{
Q_OBJECT

public:
explicit OperationMode(QObject *parent = nullptr);
~OperationMode();

Q_INVOKABLE OpsMode::ModeCRF getCRFMode();
Q_INVOKABLE void setCRFMode(OpsMode::ModeCRF md);

private:
OpsMode::ModeCRF CRFMode;
QMutex mutexMode;
};

C++:

void OperationMode::setCRFMode(OpsMode::ModeCRF md)
{
qDebug() << "Set CRF mode!" << md;
QMutexLocker locker(&mutexMode);
CRFMode = md;
}

OpsMode::ModeCRF OperationMode::getCRFMode()
{
qDebug() << "Get CRF mode!";
//QMutexLocker locker(&mutexMode);
//return CRFMode;
}

Example QML:

import MyOperationsMode 1.0
import MyOpsMode 1.0

CustomOperationsMode {
id: operationsMode
}

operationsMode.setCRFMode(CustomOpsMode.ModeIdle)


Main (registering)


OpMode = new OperationMode();
qmlRegisterType<OperationMode>("MyOperationsMode", 1, 0, "CustomOperationsMode");
qmlRegisterUncreatableType<OpsMode>("MyOpsMode", 1, 0, "CustomOpsMode", "ERROR!");

Added after 17 minutes:

Thinking this may be related: https://bugreports.qt.io/browse/QTBUG-19741

d_stranz
15th January 2020, 17:50
Thinking this may be related

Looks like it could be. But why do you derive OpsMode from QObject, when all OpsMode serves is as a scope qualifier for your enum? Why not simply declare OpsMode as a namespace? And why does the ModeCRF enum need to be separate from OperationsMode class? Can't you define it within that class?