Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize Qt resources from a shared library?

I can not figure out how to initialize Qt resources declared in and used by a shared library under Red Hat Enterprise Linux 5.2.

I added a Qt resource file to my shared library, added a prefix named "resource", and added a file "files/styleSheet.xsl". The resource file is named "resources.qrc". QFile::exists returns false?

MySharedLib::MySharedLib()
{

   // I think Q_INIT_RESOURCE basically expands to this:
   // The resource file is named "resources.qrc"
   extern int qInitResources_resources();
   qInitResources_resources(); 

      QString resourcePath = ":/resource/files/styleSheet.xsl";
      if( false == QFile::exists(resourcePath))
      {
         printf("*** Error - Resource path not found : \"%s\"\n",   resourcePath.toLatin1().data());
      }

}

Thanks in advance for any tips or suggestions,

like image 936
Ed of the Mountain Avatar asked Nov 30 '10 23:11

Ed of the Mountain


People also ask

How do I create a QRC file?

Click the Edit Resources button (first on the left) Click the New Resource File button (first on the left) Enter a file name (e.g. resources. qrc) and click Save.

What is QRC in Qt?

Qt Resource Collection File (. qrc file is an XML document that enumerates local files to be included as runtime resources. It serves as input to rcc .

What is a QRC document?

XML file used by programs created with Qt, a cross-platform application development toolkit; contains a list of application resources, such as image files; included as part of the application bundle when the Qt application is built.


1 Answers

The problem is that under Linux, you can not have identically named Qt resource files (*.qrc) in both your shared library and application. This is not a problem under Windows but under Linux it will only load one of the identically named resource files. I had named resource files in both my application and shared library files "resources.qrc". I renamed to "resourcesmylib.qrc" and "resourcesmyapp.qrc" and all was good. I did not need to add a call Q_INIT_RESOURCES to my library or call qInitResources_resources*.

Solution

  • Use unique Qt resource file names for your library and application under Linux.

Credit goes to Jaco N. on the Qt-Interest mailing list. Thank you Jaco!

like image 91
Ed of the Mountain Avatar answered Sep 22 '22 07:09

Ed of the Mountain