从插件 (.so) 加载 Qt UI(带图像)时遇到问题
我有一个插件,可以加载并显示一个自定义小部件,该小部件显示从资源文件 (resources.qrc) 加载的图像(作为 QLabel 的背景)。我面临的问题是,加载插件后,它会正确显示小部件,但不显示图像。我尝试将“Q_INIT_RESOURCE(资源)”放在任何地方,但没有任何反应。我创建了许多使用 qrc 文件来显示图像的自定义小部件,但仅直接在应用程序内,效果很好。这次是来自插件,所以这里一定是我遗漏了一些东西。有什么帮助吗?
// 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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题解决了。
问题是主应用程序已经有一个同名的 qrc 文件 (
resources.qrc
)。由主应用程序加载的插件有一个不同的resources.qrc
文件,但由于主应用程序已经有一个同名的文件,因此它没有加载该文件。我更改了插件中资源文件的名称并且工作正常。当然,我必须将Q_INIT_RESOURCE( resources );
更改为Q_INIT_RESOURCE( new_resource_file_basename );
,这是从MyWidget
类的构造函数调用的(MyWidget::MyWidget()
)。换句话说,它不需要位于插件的构造函数中 (MyPlugin::MyPlugin()
)。这是有道理的,因为MyWidget
类是使用资源文件而不是插件的类。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 differentresources.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 theQ_INIT_RESOURCE( resources );
toQ_INIT_RESOURCE( new_resource_file_basename );
which was called from the constructor of theMyWidget
class (MyWidget::MyWidget()
). In other words, it does NOT need to be in the constructor of the plugin (MyPlugin::MyPlugin()
). It makes sense, since theMyWidget
class is the one using the resource file, not the plugin.