QFontMetrics crashes with static library



  • I am developing a simple library (dll) (Qt 5.7. MinGW Windows 7, 32bit) which receives a string and some configuration parameters, and returns the length of the string:

    extern "C" STRINGCHECK_H_COMMON_DLLSPEC int get_width(QString phrase, QString font_family, int font_size, bool size_type,
                  bool kerning, bool italic, bool bold, bool merging){
        int result =  150;
        QFont current_font;
    
        if(size_type) {
            current_font.setPixelSize(font_size);
        } else {
            current_font.setPointSize(font_size);
        }
        current_font.setKerning(kerning);
        current_font.setItalic(italic);
        current_font.setBold(bold);
        current_font.setFamily(font_family);
    
        bool in_font = true;
        if(!merging) {
            current_font.setStyleStrategy(QFont::NoFontMerging);
        } else {
            current_font.setStyleStrategy(QFont::PreferDefault);
        }
        QFontMetrics q(current_font);
        for (int i=0; i<phrase.length(); i++){
            QChar curr = phrase.at(i);
            ushort code = curr.unicode();
           if (!q.inFont(code)) {
                in_font = false;
                break;
            }
        }
    
        try {
            result = q.width(phrase);
        } catch (...){
            qDebug() << "error" << result;
            result = -1;
        }
        if(!in_font) result = -result;
        return result;
    }
    
    

    Everything works fine if I build the dll with dynamic version of Qt (linking Qt's dlls ), but when I try to release a stand-alone library (always dll but whit no need of linkage to the Qt's dlls, in order to not ask other users to install Qt), the program crashes (it compiles correctly). It is surely not a problem of missing Qt's dlls since I can build other stand-alone applications which work perfectly.

    I broke down the function and found out that the QFontMetrics usage is the cause of the crash (it crashes even if I put a try-catch block). Indeed, if i comment out all the lines regarding QFontMetrics (q.inFont + q.width) the program works fine calling it from other Qt apps or also from simple C programs.

    What could be the problem? Thanks in advance.



  • Hi,
    I doubt that the compiler is able to handle by-value calls to a class parameter like QString phrase when you use extern "C". You may have more luck with a QString pointer, though, but if you want to be sure use just char *.
    -Michael.



  • I found the problem: QFontMetrics cannot run in non-GUI applications : Bug.

    So to make it run I need to build a QApplication with the comand QApplication a(arcg,argv) , indeed in an application which receveis command line arguments and then calls the get_width() function, everything works just fine.

    The problem is that if I build a dll (in the *.pro file TARGET=lib), I cannot use the constructor QApplication a(arcg,argv), since I get the error (when loading (correctly) the dll from another project and calling get_width() :

    this application failed to start because it could not find or load the qt platform plugin windows in ""

    Any way to solve this? Remember my goal is to have a dll which could run in a laptop without Qt


  • Lifetime Qt Champion

    Hi,

    You should check the details of linking static plugins in the Plugin HowTo in Qt's documentation. There might something to help you go further.

    Also, don't forget the licensing constraints coming with using a static build of Qt.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.