lundi 26 octobre 2020

Qt Connect to std::function

I'm having an issue where I'm trying to define the SIGNAL for a connection at run time, as it depends upon which derived class has created the object in question. I.e. I'm defining a base class that has an abstract slot (that the derived classes implement accordingly). Then, at the point of object construction I pass a "std::function<void(uint)>" to the base class' constructor using "std::bind(&Class::signalToPass, object, std::placeholder::_1)". I'm then trying to use "QObject::connect" to connect this std::function to the aforementioned abstract slot. I'll try and outline an example below:

The derived classes constructor:

DerviedClass(SomeClass *someObject) : BaseClass(std::bind(&SomeClass::methodToConnect, someObject, std::placeholders_1)
{
    ...
}

The base classes constructor:

BaseClass(std::function<void(uint)> methodToConnect) m_methodToConnect(methodToConnect)
{
    createConnections();
}
void BaseClass::createConnections()
{
    QObject::connect(
        this,
        m_methodToConnect,
        this,
        &BaseClass::abstractSlot);

    ...
}

Where m_methodToConnect is defined as a private std::function<void(uint)> within BaseClass.

I can get this to compile one of two ways, which both lead to runtime errors... 1st:

QObject::connect(
    this,
    SIGNAL(m_methodToConnect),
    this,
    SLOT(abstractSlot));

This results in the runtime warning:

QObject::connect: Parentheses expected, signal DerivedClass::m_methodToConnect in /home/...

Which I thought I understood so altered to this:

QObject::connect(
    this,
    SIGNAL(m_methodToConnect(uint)),
    this,
    SLOT(abstractSlot(uint)));

This results in the runtime warning:

QObject::connect: No such signal DerivedClass::m_methodToConnect(uint) in /home/...

I have also tried to pass "someObject" from the derived class to the base class and used that as the "sender" in QObject::connect, but the same result is seen. In all cases I can compile, build and install on my target, but as expected the connection isn't made.

Any help on this would be appreciated, also, as an FYI (and probably evident from the post) my knowledge of Qt's signal/slot handling is far from perfect...

Aucun commentaire:

Enregistrer un commentaire