You can not create an additional signal in qml, but use the standard properties of objects.
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Layouts 1.1
Switch{
id: swt;
checked:true;
}
C++:
class MyHandler : public QObject
{
Q_OBJECT
public:
MyHandler( const QObject& object, const QQmlProperty& qmlProperty );
private slots:
void handleNotify();
private:
const QQmlProperty& m_qmlProperty;
};
MyHandler::MyHandler( const QObject& object, const QQmlProperty& qmlProperty ) :
QObject(),
m_qmlProperty( qmlProperty )
{
static const QMetaMethod metaSlot = this->metaObject()->method( this->metaObject()->indexOfSlot( "handleNotify" ) );
if( metaSlot.isValid() )
{
const QMetaMethod metaQmlPropSignal = qmlProperty.property().notifySignal();
if( metaQmlPropSignal.isValid() )
QObject::connect( &object, metaQmlPropSignal, this, metaSlot );
}
}
MyHandler::handleNotify()
{
if( m_qmlProperty.isValid() )
{
const int qmlPropertyValue = m_qmlProperty.read().value<bool>();
...
}
}
using:
QQuickView* view = ...;
QObject* quickItem = view->rootObject();
const QQmlProperty* qmlProperty = new QQmlProperty( quickItem, "checked" );
MyHandler* myHandler = new MyHandler( *quickItem, *qmlProperty );
Thus, the method MyHandler::handleNotify
will be called when the property checked
changes (if it exists for the object quickItem
).
P.S. You can also connect a qml property with a signal.
class QmlPropertyWrapper : public QObject
{
Q_OBJECT
public:
QmlPropertyWrapper( const QObject& object, const QQmlProperty& qmlProperty );
signals:
void triggered();
};
QmlPropertyWrapper::QmlPropertyWrapper( const QObject& object, const QQmlProperty& qmlProperty ) :
QObject()
{
static const QMetaMethod metaSignal = QMetaMethod::fromSignal( &QmlPropertyWrapper::triggered );
if( metaSignal.isValid() )
{
const QMetaMethod metaQmlPropSignal = qmlProperty.property().notifySignal();
if( metaQmlPropSignal.isValid() )
QObject::connect( &object, metaQmlPropSignal, this, metaSignal );
}
}
using
QQuickView* view = ...;
QObject* quickItem = view->rootObject();
const QQmlProperty* qmlProperty = new QQmlProperty( quickItem, "checked" );
QmlPropertyWrapper* qmlPropertyWrapper = new QmlPropertyWrapper( *quickItem, *qmlProperty );
QObject::connect( qmlPropertyWrapper, &QmlPropertyWrapper::triggered, [ qmlProperty ]()
{
if( m_qmlProperty->isValid() )
{
const int qmlPropertyValue = m_qmlProperty->read().value<bool>();
...
}
} );