You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

  • "Andrei Korostelev" started this thread

Posts: 7

Location: Belarus->Netherlands

  • Send private message

1

Monday, January 29th 2007, 2:26pm

Resources in a dynamic library

I need to store resources (images, text etc) in a Qt dynamic library (.dll in Win32/.so in Linux) and make a use of them from the main Qt application.

The library will be loaded by the app. at run-time because its name/location is not know in advance. That is, I will use ::LoadLibrary/dlopen to load it the library.

How do I suppose to deal with loading resources from this library? The macro Q_INIT_RESOURCE is not appropriate since it requires the definition of qInitResources_ ##name -like functions at compile-time.

Any ideas?
-- Andrei

  • "Andrei Korostelev" started this thread

Posts: 7

Location: Belarus->Netherlands

  • Send private message

2

Monday, January 29th 2007, 3:42pm

RE: Resources in a dynamic library

I've solved the problems in the following way

1. From the dynamic library I exported the function which initializes its resources

Source code

1
2
3
4
5
int initResources()
{
    extern int qInitResources_MyLibQrcName();
    return qInitResources_MyLibQrcName();
}

where MyLibQrcName is a base name of the appropriate qrc.

2. From the app: loaded the library and called this function.
3. The resources now can be easily loaded
For example in a raw byte array

Source code

1
2
3
QFile file( ":/QrcPrefix/Resources/my_cool_image.bmp" );
file.open( QIODevice::ReadOnly );
QByteArray qstr = file.readAll();

or in QImage

Source code

1
QImage image(":/QrcPrefix/Resources/my_cool_image.bmp");


Note that the solution above works *ONLY* in case of dynamically linking against Qt libs!
Generic solutions, working for all cases:
- either export appropriate functiond from this dll which would return the required resource
- or, if the only purpose of this dll is to export resources, replace this dll with a Qt resource binary (i.e. comiled as rcc -binary myresource.qrc -o myresource.rcc. See Qt Resource System in the Qt Help for more info)
-- Andrei

This post has been edited 2 times, last edit by "Andrei Korostelev" (Apr 24th 2007, 5:03pm)