1

Has anyone tried to invoke overloaded operator << on a QObject.

For example i have a class

class Worker : public QObject
{
    Q_OBJECT
public:
    explicit Worker(QObject *parent = 0);

    Q_INVOKABLE virtual void operator<<(char p);

};

When i try to invoke it with like this i get an error:

QMetaObject::invokeMethod( &worker, QT_STRINGIFY2( operator<<(char) ), Qt::QueuedConnection, Q_ARG( char, 'a') );

ErrorMessage would be: No such method Worker::operator<<(char)(char)

4

1 回答 1

0

As noted in the docs for QMetaObject::invokeMethod:

You only need to pass the name of the signal or slot to this function, not the entire signature.

QMetaObject::invokeMethod( &worker,
                           "operator<<",
                           Qt::QueuedConnection,
                           Q_ARG( char, 'a') );

This should suffice, though I've never seen invokeMethod used on an operator before.

Edit

It appears that the moc cannot register operators into the meta-object system, calling:

qDebug() << worker.metaObject()->indexOfMethod( "operator<<" );

Will return -1. The best thing to do is put your operator<< in the base class, make it non-virtual, and have it call a new virtual Q_INVOKABLE method or slot. Derived classes would then reimplement the new method which can also be called through the meta-object system.

于 2015-03-25T10:49:31.313 回答