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. match font family return from QFont::toString()
Forum Updated to NodeBB v4.3 + New Features

match font family return from QFont::toString()

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.8k Views 4 Watching
  • 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.
  • U Offline
    U Offline
    user4592357
    wrote on last edited by
    #1

    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: https://regex101.com/r/rXDN3L/1, but not in my code. what can be the reason?

    raven-worxR kshegunovK 2 Replies Last reply
    0
    • U user4592357

      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: https://regex101.com/r/rXDN3L/1, but not in my code. what can be the reason?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

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

      what can be the reason?

      your code probably.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

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

        Hi,

        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.

        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
        • U Offline
          U Offline
          user4592357
          wrote on last edited by
          #4

          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();
          }
          
          1 Reply Last reply
          0
          • U user4592357

            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: https://regex101.com/r/rXDN3L/1, but not in my code. what can be the reason?

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by kshegunov
            #5

            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.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            3
            • U Offline
              U Offline
              user4592357
              wrote on last edited by
              #6

              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

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

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

                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
                1

                • Login

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