One of the most central mechanisms in QML are properties and property bindings.
Thus one of the best ways to make a value, especially one that gets modified during runtime, accessible from C++ is to define a property of a C++ class.
{
Q_OBJECT
Q_PROPERTY(bool reachable READ reachable NOTIFY reachableChanged)
public:
bool reachable() const; // property getter function
signals:
void reachableChanged(); // property change notification signal
};
class UrlChecker : public QObject
{
Q_OBJECT
Q_PROPERTY(bool reachable READ reachable NOTIFY reachableChanged)
public:
bool reachable() const; // property getter function
signals:
void reachableChanged(); // property change notification signal
};
To copy to clipboard, switch view to plain text mode
Such a class can then be registered with the QML type system
qmlRegisterType<UrlChecker>("MyTypes", 1, 0, "UrlChecker");
qmlRegisterType<UrlChecker>("MyTypes", 1, 0, "UrlChecker");
To copy to clipboard, switch view to plain text mode
And used in QML like any other type
import QtQuick 2.0
import MyTypes 1.0
Item {
UrlChecker {
id: checker
}
Text {
text: checker.reachable ? "URL reachable" : "URL not reachable"
}
}
import QtQuick 2.0
import MyTypes 1.0
Item {
UrlChecker {
id: checker
}
Text {
text: checker.reachable ? "URL reachable" : "URL not reachable"
}
}
To copy to clipboard, switch view to plain text mode
Cheers,
_
Bookmarks