Вопрос

Typically Qt signals are triggered on a specific instance of a QObject.

Is there a way to trigger it on all instances of a specific class via, perhaps, MetaObjects?

Or the only way is to maintain your own static list of all instances, perhaps by using constructors and destructors, and then just Q_FOREACH it?

Это было полезно?

Решение

How about creating a singleton behind the scenes, and connecting all your instances to a signal from that singleton (signal-to-signal connection)? When you want all your instances to emit the signal, just make the singleton emit it, and all instances will forward it.

Другие советы

Signals cannot be emitted outside of the class that defines them (or derives from the class that defines them) without invoking it through the QMetaObject system:

QMetaObject::invokeMethod( myObj, "mySignal",
                           Q_ARG( QString, "str" ),
                           Q_ARG( int, 42 ) );

However there doesn't appear to be an API method of getting all objects of all particular type to emit, the nearest I could find is:

for ( QWidget* widget : QApplication::allWidgets() ) {
    if ( dynamic_cast< myType* >( widget ) ) {
        QMetaObject::invokeMethod( widget, "mySignal",
                                   Q_ARG( QString, "str" ),
                                   Q_ARG( int, 42 ) );
    }
}

But obviously this only works for QWidget derived types, there doesn't appear to be a QObject equivalent.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top