April 18, 2012

theunsheydenrych theunsheyden..
Lab Rat
2 posts

QFontDatabase falls over in c++ library

 

hi
I developed a c++ library with Qt Creator for linux that generate png images from svg files, these images is then passsed back to the caller in a byte buffer.
I also written a c++ application with Qt Creator that uses this library for testing purposes, to verify that the library is working correctly.
I then created proxy library for java via swig, to utilise the library from a java application to create images/png’s via the c++ library.

Everything work correctly in the Qt Creator application that uses the library.
When uses the library from java, the application falls over, when a svg file with a text element have to be rendered.

The error output that i get from java, hints that the code is falling over in the c++ library when parsing fontnames in the QFontDatabase. This happens when the render method on the QSvgRenderer is executed.
Below is a copy of some of the error when using the library from java output:
“Problematic frame:
C [libQtGui.so.4+0×3ef16b] QFontDatabase::parseFontName(QString const&, QString&, QString&)+0×21b “

I then added a local variable in the c++ library method where the i am rendering the svg as follows, in order to locate the problem:

  1.         QFont font = fdb.font("Arial","bold",9);
  2.         qDebug() << "C++ API: font rawname:" + font.family() ;

When using the c++ library with above code through the Qt application, it works 100%.
When using the c++ library from java, it falls over exactly when trying to contruct the QFontDatabase above.

All i can think, what the problem is, is that somehow, somewhere “pre initialasing” stuff is happening for the QFontDatabase when using it from the Qt application, and not the same stuff “happened” when using the library from java?
It looks like the QFontDatabase code rezides in the libQtGui.so.4 library, maybe i have to do additional calls in my c++ library to get the QFontDatabase to construct correctly?
I have a feeling/suspision that something in QtGui have to be initialsed for the QFontDatabase, to work correctly when the c++ library is called from java or any other language, but i have no idea what?

The svg file is very simple it looks like this:
<!DOCTYPE svg PUBLIC ‘-//W3C//DTD SVG 1.1//EN’ ‘http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd’>
<svg width=“400px” height=“400px” viewBox=“0 0 400 400” id=“S-GPUC——*****”> <g id=“group_scale” transform=“scale(1)”> <g> <text x=“200” y=“232” >CBT</text> </g> </g>
</svg>

Any svg document with a text element in, let the application crash with:
“Problematic frame:
C [libQtGui.so.4+0×3ef16b] QFontDatabase::parseFontName(QString const&, QString&, QString&)+0×21b”

All other svg documents without text elements renders correctly.

Any help or suggestions will be highly appreciated.
Thank you in advance.

PS.
I am using the following:
Qt Creator 2.1.0
Based on Qt 4.7.2 (64 bit)

Operating System:
Ubuntu 11.04 64bit

1 reply

April 19, 2012

theunsheydenrych theunsheyden..
Lab Rat
2 posts

I found the problem.
The SVG rendering part wants to get the “Arial” font from the QFontDatabase, and the QFontDatabase is not initialized. QFontDatabase is part of the QApplication, and QApplication handles the initialization of these things, according to the help on this page.
http://qt-project.org/doc/qt-4.8/QApplication.html#details

And i quote:
QApplication’s main areas of responsibility are:
It initializes the application with the user’s desktop settings such as palette(), font() and doubleClickInterval(). It keeps track of these properties in case the user changes the desktop globally, for example through some kind of control panel.

The solution:
Add a method to the library to initialize QApplication.

  1. SYMGENAPISHARED_EXPORT void initLibrary()
  2. {
  3.     QCoreApplication *app = QApplication::instance();
  4.     if(app == NULL)
  5.     {
  6.        app = new QApplication(0,NULL);
  7.     }
  8. }

The problem now is how do you release the memory when the shared library is not used any more?

 
  ‹‹ (Solved) Focus in table widget      Using GPS in Qt applications ››

You must log in to post a reply. Not a member yet? Register here!