Home:ALL Converter>QObject::connect: Cannot connect (null)

QObject::connect: Cannot connect (null)

Ask Time:2011-12-14T01:30:12         Author:develoops

Json Formatter

I try to load to plugins. If they are loaded with success , then i must connect returned widgets.

With one plugin i create an action and add it to a menu , with another plugin i create a label and add it to window. Even if i get this error during runtime (when app loads plugins) , these two widgets are created and are visible. But there is no connection between them.

This is how i try to connect widgets

QObject *plugin = pluginLoader.instance();
if (plugin) {
    myAction = qobject_cast<ActionInterface *>(plugin);

    if (myAction) {
        pluginMenu->addAction(myAction->newAction());
        verify ++;
    }

    myLabel = qobject_cast<LabelInterface *>(plugin);

    if (myLabel) {            
        layout->addWidget(myLabel->newLabel());
        verify++;
    }

    if (verify == 2)
        connect(myAction, SIGNAL(pushMyAction()),
            myLabel, SLOT(setTextforLabel()));
    }

    ...
}

Error message is :

QObject::connect: Cannot connect (null)::pushMyAction() to LabelPlugin::setTextforLabel()

Author:develoops,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/8493506/qobjectconnect-cannot-connect-null
HostileFork says dont trust SE :

You have two different plugins. Apparently one can be cast to an ActionInterface but not a LabelInterface, and the other can be cast to a LabelInterface but not an ActionInterface.\n\nYour idea here seems to be that once you have both plugins loaded (and a verify count of 2) then it's safe to make a connect call between these plugins. However you appear to be trying to cast the second loaded plugin to serve as both the signal and the slot. This is because each time you run the code you overwrite both myAction and myLabel. So at minimum:\n\nQObject* plugin = pluginLoader.instance();\nif (plugin) {\n ActionInterface* myActionTemp = qobject_cast<ActionInterface*>(plugin);\n\n if (myActionTemp) {\n myAction = myActionTemp;\n pluginMenu->addAction(myAction->newAction());\n verify++;\n }\n\n LabelInterface* myLabelTemp = qobject_cast<LabelInterface*>(plugin);\n\n if (myLabelTemp) {\n myLabel = myLabelTemp; \n layout->addWidget(myLabel->newLabel());\n verify++;\n }\n\n /* if (myAction and myLabel) would be less convoluted... */\n if (verify == 2) { \n connect(myAction, SIGNAL(pushMyAction()),\n myLabel, SLOT(setTextforLabel()));\n }\n\n ...\n}\n\n\nStill, this looks like a fairly brittle design that could use some rethinking...!",
2011-12-13T18:15:32
yy