问题
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