Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFontMetrics crashes with static library
QtWS25 Last Chance

QFontMetrics crashes with static library

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CBenussi
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • m.sueM Offline
      m.sueM Offline
      m.sue
      wrote on last edited by m.sue
      #2

      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.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        CBenussi
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved