Trouble loading Qt UI (with images) from plugin (.so)

风格不统一 提交于 2019-12-22 10:35:18

问题


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)

回答1:


Problem solved.

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.



来源:https://stackoverflow.com/questions/5097087/trouble-loading-qt-ui-with-images-from-plugin-so

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