Pergunta

How do I use a foreach loop on a QSignalSpy?

This is what I want to do:

foreach( const QList<QVariant> &args, mySignalSpy )
{
    Q_ASSERT( args.count() == 2 );
    QVariant arg0 = args[0];
    QVariant arg1 = args[1];
    doSomething( arg0, arg1 );
}

This is the error I get:

/usr/local/Trolltech/Qt-4.8.2-shared/include/QtTest/qsignalspy.h: In instantiation of ‘QForeachContainer<T>::QForeachContainer(const T&) [with T = QSignalSpy]’:
MyTester.cxx:843:64:   required from here
/usr/local/Trolltech/Qt-4.8.2-shared/include/QtCore/qobject.h:333:5: error: ‘QObject::QObject(const QObject&)’ is private
Foi útil?

Solução

foreach makes a copy of passed container. QSignalSpy inherits QObject hence it cannot be copied. These two facts cause the error.

The simpliest workaround is to use usual for loop. You can also create a non-QObject copy of the list and use it to iterate:

QList< QList<QVariant> > list = mySignalSpy;
foreach( const QList<QVariant> &args, list ) {
  //...
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top