从插件 (.so) 加载 Qt UI(带图像)时遇到问题

发布于 2024-10-19 10:56:12 字数 817 浏览 1 评论 0原文

我有一个插件,可以加载并显示一个自定义小部件,该小部件显示从资源文件 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜爱皱眉﹌ 2024-10-26 10:56:12

问题解决了。

问题是主应用程序已经有一个同名的 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文