1

如何foreach在 a 上使用循环QSignalSpy

这就是我想要做的:

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

这是我得到的错误:

/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
4

1 回答 1

8

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 ) {
  //...
}
于 2013-08-02T20:23:53.147 回答