How to use QMetaMethod with QObject :: connect

I have two instances of the QObject subclass and two instances of the QMetaMethod signal in one of the objects and a slot in the other object. I want to connect this signal and slot to each other.

I looked at the qobject.h file and found that the SIGNAL () and SLOT () macros simply add the character "1" or "2" to the beginning of the method signature, so it seems like it should be possible to add the same character to the beginning of the line returned by QMetaMethod :: signature (), but this approach depends on some undocumented internal components of the toolkit and may be violated at any time by the new version of Qt.

Does anyone know a reliable way to connect signals and slots through their QMetaMethod reflection representation?

Edited by: I created an offer in Qt issue tracker: https://bugreports.qt.io/browse/QTBUG-10637 If anyone is interested in this feature, you can vote for this ticket there.

+5
source share
3 answers

It seems that there is no way to make it work without relying on internal implementation. If I were you, I sent a request to the Qt tracker function , write a code that mimics the current behavior of the SIGNAL / SLOT macros and adds a unit test that will not work when the SIGNAL / SLOT behavior changes.

A simpler solution to the problem you are trying to solve may arise: describe what exactly you are trying to do without any implementation details.

+1

Qt 4.8.0:

https://bugreports.qt.io/browse/QTBUG-10637

, QObject * m_subject propertyChanged():

const QMetaObject* meta = m_subject->metaObject();
QMetaProperty prop = meta->property(meta->indexOfProperty("myProperty"));
if (prop.hasNotifySignal()) {
    QMetaMethod signal = prop.notifySignal();
    QMetaMethod updateSlot = metaObject()->method(
        metaObject()->indexOfSlot("propertyChanged()"));
    connect(m_subject, signal, this, updateSlot);
}

QWidget, QObject QLineEdit , , QLineEdit , . ( propertyID Changed(), QLineEdit property() . QSignalMapper , .)

+11

If the signature method is publicly available in QMetaMethod, then the result should not be broken by trolls and be safe to use (the documentation says nothing about the “dangers” when using the QMetaMethod :: signature method). I think you can safely use it. To be sure which version of Qt you are using right now?

0
source

All Articles