I\'ve successfully exposed a C++ class to QML. It is registered and found in Qt Creator. It\'s purpose is to connect to a database, as shown in following code:
Be aware in case of an error some lines earlier in the same .js file can lead to this problem. The following code will not be executed.
You must first create the object you want in QML code and then use the function:
your QML Code:
import QtQuick 2.0
import QSqlDatabase 1.0
Rectangle {
id: ueMenuButton
QSqlDatabase {
id: uePosDatabase
}
.
.
.
MouseArea
{
id: ueClickArea
antialiasing: true
anchors.fill: parent
onClicked: {
console.log(uePosDatabase.isConnected())
}
}
}
You have this error because you have declared the property isConnected
in C++ but you're calling it from QML in the wrong way: uePosDatabase.isConnected
is the correct way, not uePosDatabase.isConnected()
.
If you want to call the function isConnected()
you should change its name to differate it from the property, like getIsConnected()
. Given your property declaration, you neither need to call this function directly nor you need to make it callable from QML
with the Q_INVOKABLE
macro.