简体   繁体   中英

How to execute QML slot from C++ without a signal?

I got a QML object in the QObject shape. With ->setProperty(..., ...) I can change properties, but how can I execute slots without signals?

Currently I declare a signal in my C++ class:

signals:
  void testSignal();

and signal/slot in QML object:

signal executeTestFunc(); onExecuteTestFunc: {
  testAnimation.start();
}

Then I connect these two:

QObject::connect(this, SIGNAL(testSignal()),
                 QmlObject, SIGNAL(executeTestFunc()));

But this is not clean, as I can see:

  1. De facto this is strange SIGNAL/SIGNAL connection.
  2. I do not want to use SIGNAL/SLOT mechanism, unless I have to, due to performance and long code.

Is there a way to execute QML onExecuteTestFunc(); from QObject directly?

You can create a C++ class that will emit a signal. That signal will be caught in QML. No explicit connection with SIGNAL/SLOT is needed. Example:

C++:

class Presenter : public QObject
{
    Q_OBJECT
public:
    explicit Presenter()
    {
        QTimer *timer = new QTimer();
        timer->setInterval(500);
        connect(timer, &QTimer::timeout,
                this, &Presenter::timeout);
        timer->start();
    }

    Q_SIGNAL void timeout();
};

QML:

Window {
    ...
    Presenter {
        onTimeout: {
            console.log("called from c++")
        }
    }
}

Result:

qml: called from c++
qml: called from c++
qml: called from c++
qml: called from c++
...

@Thomenson has already given a good answer on how you can connect to a signal from C++ and act on it in QML. His solution works if you can create the C++ from QML, which may not always be the case.

Connections element

There are two other options you might consider. First, if you have an object that was not created from QML but was put into it in some other way (QML context, static object, returned from an invokable...) you may use the Connections element:

Connections {
    target: theObjectWithTheSignal
    onSignalName: {doSomething();}
}

This is a flexible way to react to signals from object that you did not create in QML, or even objects that were created elsewhere in QML.

Callback using JSValue

If you really insist on avoiding signal/slot (though I don't understand why; Qt and QML are build around it and trying to avoid it is fighting against the framework instead of using its strenghts), there is another way. You can, on the C++ object that is exposed to QML, create a property of type QJSValue . Then, in C++ on setting, check that whatever was set is callable (using QJSValue::isCallable() ). Then as the point you wish to trigger whatever you want to execute in your QML, call it using QJSValue::call .

On the QML side, you can simply assign or bind something callable, just like you'd do for a signal handler.

Anti pattern: QMetaObject::invokeMethod

There is another way, which I will only include as a warning against an anti-pattern. You can call into the QML from C++ using Qt's introspection mechanism. You can find an object by it's set objectName and call any (invokable) method on it using QMetaObject::invokeMethod, read and write any property and listen to all signals. That includes calling methods you defined in QML. Using this, you can manipulate your QML from your C++. Don't do this. It leads to inflexible and unmaintainable code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM