QObject::connect: Cannot connect (null)

陌路散爱 提交于 2019-12-11 23:21:51

问题


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()


回答1:


QObject *plugin = pluginLoader.instance();
if (plugin) {

    if (plugin->inherits("ActionInterface")) {
        myAction = qobject_cast<ActionInterface *>(plugin);
        pluginMenu->addAction(myAction->newAction());
    }

    if (plugin->inherits("LabelInterface")) {            
        myLabel = qobject_cast<LabelInterface *>(plugin);
        layout->addWidget(myLabel->newLabel());
    }

    if (myLabel && myAction)
        connect(myAction, SIGNAL(pushMyAction()),
            myLabel, SLOT(setTextforLabel()));
    }

    ...
}

Remove this "ugly" verify counter. Remember to initialize myLabel and myAction with NULL




回答2:


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.

Your 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:

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

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

    LabelInterface* myLabelTemp = qobject_cast<LabelInterface*>(plugin);

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

    /* if (myAction and myLabel) would be less convoluted... */
    if (verify == 2) { 
        connect(myAction, SIGNAL(pushMyAction()),
            myLabel, SLOT(setTextforLabel()));
    }

    ...
}

Still, this looks like a fairly brittle design that could use some rethinking...!



来源:https://stackoverflow.com/questions/8493506/qobjectconnect-cannot-connect-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!