match font family return from QFont::toString()

  • so i have a QFont which i convert to QString using toString() which returns something like "Console,12,-2,5,50,0,0,0,0,0". i need to parse this to get the font family. so i tried this regex: ^([^,]*)
    which works on this website:, but not in my code. what can be the reason?

  • Moderators

    @user4592357 said in match font family return from QFont::toString():

    what can be the reason?

    your code probably.

  • Lifetime Qt Champion


    Please, show your code and the Qt version you are using. This expression works both with QRegularExpression (which you should be using) and the deprecated QRegExp.

  • i'm using qt4. here's the code:

    #include <iostream>
    #include <QApplication>
    #include <QString>
    #include <QRegExp>
    int main(int argc, char **argv) {
        QApplication app(argc, argv);
        QString font = "Console,12,-2,5,50,0,0,0,0,0";
        QRegExp fontFamilyRegExp("^([^,]*)");
        if(fontFamilyRegExp.exactMatch(font)) {
            std::cout << fontFamilyRegExp.cap(1).toStdString(); // should return "Courier" but doesn't
        return app.exec();

  • Moderators

    Why are you doing this?
    What you've shown is the internal (serialized) representation of a font and you shouldn't mess with it directly. Also, since I did a bit of work on the fonts several months ago I can tell you that string also has different versions. Unless you intend to hack into Qt's core and support all those then you should adopt another approach.

    Use QFont::fromString to get a QFont instance from the string and then you can query the family simply by QFont::family.

    PS. Unless there's a very specific reason to use Qt 4 you ought to update your code and use Qt 5.

  • yes there is a reason why i use qt4.

    well, that worked, thanks.

    but anyways i wonder how i could do that with regexps. also, how could i get the second argument (which is returned pointSIze() analogously to family()) with regexp? i've tried things and none worked

  • Lifetime Qt Champion

    Here's your problem: exactMatch, you don't have an exact match since you only want to capture the first part of the string.

Log in to reply