문제

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
도움이 되었습니까?

해결책

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 ) {
  //...
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top