无法从插件(.so)加载Qt UI(带有图像) [英] Trouble loading Qt UI (with images) from plugin (.so)

查看:208
本文介绍了无法从插件(.so)加载Qt UI(带有图像)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个加载并显示自定义窗口小部件的插件,该窗口小部件显示从资源文件(resources.qrc)加载的图像(作为QLabel的背景).我面临的问题是,插件加载后,它会正确显示窗口小部件,但不会显示图像.我尝试将"Q_INIT_RESOURCE(resources)"放在任何地方,但是什么也没有发生.我创建了许多使用qrc文件显示图像的自定义窗口小部件,但仅在应用程序内直接显示,效果很好.这次是从插件开始的,所以这里一定缺少我想要的东西.有帮助吗?

I have a plugin that loads and shows a custom widget that displays an image (as a background for a QLabel) loaded from a resource file (resources.qrc). The problem I'm facing is that once the plugin is loaded, it shows the widget properly, but not the image. I tried putting "Q_INIT_RESOURCE( resources )" everywhere, but nothing happens. I have created many custom widgets that use qrc files to display images, but only directly within an app, which have worked just fine. This time is from a plugin, so there must be something I'm missing here. Any help?

// TheInterface.h
class TheInterface
{
    ...
}
Q_DECLARE_INTERFACE(TheInterface,"com.system.subsystem.TheInterface/1.0");



// MyWidget.h
class MyWidget : public QWidget, public Ui::MyWidget
{
    Q_OBJECT
    ...
}



// MyPlugin.h
#include "TheInterface.h"
class MyPlugin : public QOBject,
                 public TheInterface
{
    Q_OBJECT
    Q_INTERFACES(TheInterface)

    ...
};

// MyPlugin.cpp
#include "MyPlugin.h"
#include "MyWidget.h"
MyPlugin::MyPlugin()
{
    MyPlugin* w = new MyPlugin();
    w->show();
}

Q_EXPORT_PLUGIN2(myplugin, MyPlugin)

推荐答案

问题已解决.

问题是主应用程序已经有一个同名(resources.qrc)的qrc文件.插件(由主应用程序加载)具有不同的resources.qrc文件,但是由于主应用程序已经具有相同的名称,因此未加载该文件.我在插件中更改了资源文件的名称,并且运行良好.当然,我必须将Q_INIT_RESOURCE( resources );更改为Q_INIT_RESOURCE( new_resource_file_basename );,这是从MyWidget类(MyWidget::MyWidget())的构造函数调用的.换句话说,它不必位于插件的构造函数(MyPlugin::MyPlugin())中.这是有道理的,因为MyWidget类是使用资源文件而不是插件的类.

The problem was that the main application had already a qrc file with the same name (resources.qrc). The plugin --being loaded by the main app-- has a different resources.qrc file, but because the main app had one already with the same name, it was not loading it. I changed the name of the resource file in the plugin and worked perfectly. Of course, I had to change the Q_INIT_RESOURCE( resources ); to Q_INIT_RESOURCE( new_resource_file_basename ); which was called from the constructor of the MyWidget class (MyWidget::MyWidget()). In other words, it does NOT need to be in the constructor of the plugin (MyPlugin::MyPlugin()). It makes sense, since the MyWidget class is the one using the resource file, not the plugin.

这篇关于无法从插件(.so)加载Qt UI(带有图像)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆